Backend Development 11 min read

Django Views: HttpRequest, HttpResponse, render and redirect Functions

This article explains Django view fundamentals, covering view functions, HttpRequest and HttpResponse objects, the render and redirect helpers, their differences, and provides practical code examples illustrating their usage in web applications today.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Django Views: HttpRequest, HttpResponse, render and redirect Functions

A view function in Django is a simple Python function that receives a web request and returns a web response. The response can be HTML, a redirect, an error, XML, an image, or any other data. Regardless of the view's internal logic, it must always return an HttpResponse object. By convention, view functions are placed in a file named views.py within the project or application directory.

The HttpRequest object contains all information about the incoming request. Key attributes include: request.path (the requested path), request.method (HTTP method such as GET or POST), request.body (raw request body as bytes), request.GET and request.POST (dictionary‑like objects for query string and form data), request.COOKIES , request.FILES (uploaded files), request.user (current user or AnonymousUser), and request.session (session dictionary). Methods like get_full_path() return the full URL including query string, and request.POST.getlist() retrieves multiple values for checkboxes or selects.

The HttpResponse object must be created by the developer; every view is responsible for returning an instance of this class. Common ways to generate an HttpResponse include directly instantiating it with HTML strings, using the render shortcut to combine a template with a context, or using the redirect shortcut to send an HTTP redirect.

The render function signature is render(request, template_name[, context]) . It loads the specified template, fills it with the provided context dictionary, and returns an HttpResponse containing the rendered HTML. Example:

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

Another example using render from django.shortcuts :

from django.shortcuts import render

def test(request):
    return render(request, 'index.html')  # displays an HTML page

The redirect function can accept a model instance, a view name (with optional arguments), or a hard‑coded/absolute URL. By default it returns a temporary redirect (status 302); passing permanent=True yields a permanent redirect (status 301). Usage examples:

from django.shortcuts import redirect

def my_view(request):
    object = MyModel.objects.get(...)
    return redirect(object)               # calls get_absolute_url()
    return redirect('some-view-name', foo='bar')  # reverse resolves URL
    return redirect('/some/url/')         # hard‑coded path
    return redirect('http://example.com/') # absolute URL
    return redirect(object, permanent=True) # permanent redirect

Comparison of render and redirect :

render : returns the rendered page content immediately; no additional HTTP request is triggered, so the browser URL stays the same and any subsequent refresh repeats the same view.

redirect : instructs the browser to make a second request to the given URL, updating the address bar and allowing the target URL’s view function to run (including any template rendering).

In short, render is for displaying a template directly, while redirect is for sending the client to a different URL, triggering a new request‑response cycle.

DjangoViewshttpRequestHttpResponseredirectrender
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.