Writing views

    Here’s a view that returns the current date and time, as an HTML document:

    Let’s step through this code one line at a time:

    • First, we import the class HttpResponse from the module, along with Python’s datetime library.

    • Next, we define a function called current_datetime. This is the view function. Each view function takes an HttpRequest object as its first parameter, which is typically named request.

      Note that the name of the view function doesn’t matter; it doesn’t have to be named in a certain way in order for Django to recognize it. We’re calling it current_datetime here, because that name clearly indicates what it does.

    • The view returns an object that contains the generated response. Each view function is responsible for returning an HttpResponse object. (There are exceptions, but we’ll get to those later.)

    Django’s Time Zone

    Django includes a setting that defaults to America/Chicago. This probably isn’t where you live, so you might want to change it in your settings file.

    So, to recap, this view function returns an HTML page that includes the current date and time. To display this view at a particular URL, you’ll need to create a URLconf; see URL dispatcher for instructions.

    1. from django.http import HttpResponse, HttpResponseNotFound
    2. def my_view(request):
    3. # ...
    4. return HttpResponseNotFound('<h1>Page not found</h1>')
    5. else:
    6. return HttpResponse('<h1>Page was found</h1>')

    There isn’t a specialized subclass for every possible HTTP response code, since many of them aren’t going to be that common. However, as documented in the documentation, you can also pass the HTTP status code into the constructor for HttpResponse to create a return class for any status code you like. For example:

    1. from django.http import HttpResponse
    2. # ...
    3. # Return a "created" (201) response code.
    4. return HttpResponse(status=201)

    Because 404 errors are by far the most common HTTP error, there’s an easier way to handle those errors.

    class django.http.Http404

    When you return an error such as , you’re responsible for defining the HTML of the resulting error page:

    For convenience, and because it’s a good idea to have a consistent 404 error page across your site, Django provides an Http404 exception. If you raise Http404 at any point in a view function, Django will catch it and return the standard error page for your application, along with an HTTP error code 404.

    Example usage:

    1. from django.http import Http404
    2. from django.shortcuts import render
    3. from polls.models import Poll
    4. try:
    5. p = Poll.objects.get(pk=poll_id)
    6. except Poll.DoesNotExist:
    7. return render(request, 'polls/detail.html', {'poll': p})

    In order to show customized HTML when Django returns a 404, you can create an HTML template named 404.html and place it in the top level of your template tree. This template will then be served when DEBUG is set to False.

    When is True, you can provide a message to Http404 and it will appear in the standard 404 debug template. Use these messages for debugging purposes; they generally aren’t suitable for use in a production 404 template.

    The default error views in Django should suffice for most web applications, but can easily be overridden if you need any custom behavior. Specify the handlers as seen below in your URLconf (setting them anywhere else will have no effect).

    1. handler404 = 'mysite.views.my_custom_page_not_found_view'

    The server_error() view is overridden by :

    The permission_denied() view is overridden by :

    1. handler403 = 'mysite.views.my_custom_permission_denied_view'

    The bad_request() view is overridden by :

    1. handler400 = 'mysite.views.my_custom_bad_request_view'

    See also

    Use the CSRF_FAILURE_VIEW setting to override the CSRF error view.

    Testing custom error views

    To test the response of a custom error handler, raise the appropriate exception in a test view. For example:

    As well as being synchronous functions, views can also be asynchronous (“async”) functions, normally defined using Python’s async def syntax. Django will automatically detect these and run them in an async context. However, you will need to use an async server based on ASGI to get their performance benefits.

    Here’s an example of an async view:

    1. import datetime
    2. from django.http import HttpResponse
    3. async def current_datetime(request):
    4. now = datetime.datetime.now()
    5. html = '<html><body>It is now %s.</body></html>' % now

    You can read more about Django’s async support, and how to best use async views, in Asynchronous support.