Ticket #2906: request_response.diff

File request_response.diff, 2.9 KB (added by Marc Fargas <telenieko@…>, 18 years ago)

docs.

  • docs/request_response.txt

     
    432432
    433433``HttpResponseServerError``
    434434    Acts just like ``HttpResponse`` but uses a 500 status code.
     435
     436Returning Errors
     437================
     438
     439Returning error codes in Django is easy, as you saw some lines above there
     440are the ``HttpResponseNotFound``, ``HttpResponseForbidden``,
     441``HttpResponseServerError``, etc. subclasses which, when returned by a view,
     442will make the Web server return the corresponding error codes (404, 403, 500,
     443...)
     444
     445But there are more things you can do when returning erros, like calling a
     446personalized view, returning a custom template, ...
     447
     448The Http404 exception
     449---------------------
     450There is an exception in Django that you can raise in your views, it's does
     451the same thing as returning a ``HttpResponseNotFound``, but from the world of
     452exceptions.
     453
     454    from django.http import Http404
     455    # ...
     456    def detail(request, poll_id):
     457        try:
     458            p = Poll.objects.get(pk=poll_id)
     459        except Poll.DoesNotExist:
     460            raise Http404
     461        return render_to_response('polls/detail.html', {'poll': p})
     462
     463Error views
     464~~~~~~~~~~~
     465
     466The 404 (page not found) view
     467-----------------------------
     468
     469When you raise ``Http404`` from within a view, Django will load a special view
     470devoted to handling 404 errors. It finds it by looking for the variable
     471``handler404``, which is a string in Python dotted syntax -- the same format
     472the normal URLconf callbacks use. A 404 view itself has nothing special: It's
     473just a normal view.
     474
     475You normally won't have to bother with writing 404 views. By default, URLconfs
     476have the following line up top::
     477
     478    from django.conf.urls.defaults import *
     479
     480That takes care of setting ``handler404`` in the current module. As you can see
     481in ``django/conf/urls/defaults.py``, ``handler404`` is set to
     482``'django.views.defaults.page_not_found'`` by default.
     483
     484Three more things to note about 404 views:
     485
     486    * The 404 view is also called if Django doesn't find a match after checking
     487      every regular expression in the URLconf.
     488    * If you don't define your own 404 view -- and simply use the default,
     489      which is recommended -- you still have one obligation: To create a
     490      ``404.html`` template in the root of your template directory. The default
     491      404 view will use that template for all 404 errors.
     492    * If ``DEBUG`` is set to ``True`` (in your settings module) then your 404
     493      view will never be used, and the traceback will be displayed instead.
     494
     495The 500 (server error) view
     496---------------------------
     497
     498Similarly, URLconfs may define a ``handler500``, which points to a view to call
     499in case of server errors. Server errors happen when you have runtime errors in
     500view code.
Back to Top