Django

Code

Changeset 4534

Show
Ignore:
Timestamp:
02/16/07 22:59:49 (2 years ago)
Author:
mtredinnick
Message:

Fixed #2906 -- Added documentation about HTTP error code returns and the
default 404 and 500 code error handlers. Based on a patch from Marc Fargas.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/request_response.txt

    r4420 r4534  
    433433``HttpResponseServerError`` 
    434434    Acts just like ``HttpResponse`` but uses a 500 status code. 
     435 
     436Returning Errors 
     437================ 
     438 
     439Returning HTTP error codes in Django is easy; there are the 
     440``HttpResponseNotFound``, ``HttpResponseForbidden``, 
     441``HttpResponseServerError``, etc. subclasses mentioned above which, when 
     442returned by a view, will make the Web server return the corresponding error 
     443codes (404, 403, 500, ...) and HTTP headers. 
     444 
     445The Http404 exception 
     446--------------------- 
     447When you return an error such as ``HttpResponseNotFound``, you are responsible 
     448for returning the error page and everything yourself. Since this extra 
     449information will normally be fairly uniform across your site and because you 
     450often want to bail out of the middle of a view with a quick "content not 
     451found" error, Django provides the ``Http404`` exception. This exception is 
     452caught by Django and results in the standard error page for your application 
     453being returned along with a 404 error code (although this behavior can be 
     454customised, as described below). 
     455 
     456Using this exception in your code would look something like:: 
     457 
     458    from django.http import Http404 
     459    # ... 
     460    def detail(request, poll_id): 
     461        try: 
     462            p = Poll.objects.get(pk=poll_id) 
     463        except Poll.DoesNotExist: 
     464            raise Http404 
     465        return render_to_response('polls/detail.html', {'poll': p}) 
     466 
     467In order to use the ``Http404`` exception to its fullest, you should create a 
     468template that is displayed when a 404 error is raised. This template should be 
     469called ``404.html`` and located in the top level of your template tree. 
     470 
     471Customing error views 
     472--------------------- 
     473 
     474The 404 (page not found) view 
     475~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     476 
     477When you raise the ``Http404`` exception, Django will load a special view 
     478devoted to handling 404 errors. It finds it by looking for the variable 
     479``handler404``, which is a string in Python dotted syntax -- the same format 
     480the normal URLconf callbacks use. A 404 view itself has nothing special: It's 
     481just a normal view. 
     482 
     483You normally won't have to bother with writing 404 views. By default, URLconfs 
     484contain the following line:: 
     485 
     486    from django.conf.urls.defaults import * 
     487 
     488That takes care of setting ``handler404`` in the current module. As you can see 
     489in ``django/conf/urls/defaults.py``, ``handler404`` is set to 
     490``'django.views.defaults.page_not_found'`` by default. 
     491 
     492Three things to note about 404 views: 
     493 
     494    * The 404 view is also called if Django doesn't find a match after checking 
     495      every regular expression in the URLconf. 
     496    * If you don't define your own 404 view -- and simply use the default, 
     497      which is recommended -- you still have one obligation: To create a 
     498      ``404.html`` template in the root of your template directory. The default 
     499      404 view will use that template for all 404 errors. 
     500    * If ``DEBUG`` is set to ``True`` (in your settings module) then your 404 
     501      view will never be used, and the traceback will be displayed instead. 
     502 
     503The 500 (server error) view 
     504~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     505 
     506URLconfs may also define a ``handler500``, which points to a view to call in 
     507case of server errors. Server errors happen when you have runtime errors in 
     508view code.