| | 435 | |
|---|
| | 436 | Returning Errors |
|---|
| | 437 | ================ |
|---|
| | 438 | |
|---|
| | 439 | Returning HTTP error codes in Django is easy; there are the |
|---|
| | 440 | ``HttpResponseNotFound``, ``HttpResponseForbidden``, |
|---|
| | 441 | ``HttpResponseServerError``, etc. subclasses mentioned above which, when |
|---|
| | 442 | returned by a view, will make the Web server return the corresponding error |
|---|
| | 443 | codes (404, 403, 500, ...) and HTTP headers. |
|---|
| | 444 | |
|---|
| | 445 | The Http404 exception |
|---|
| | 446 | --------------------- |
|---|
| | 447 | When you return an error such as ``HttpResponseNotFound``, you are responsible |
|---|
| | 448 | for returning the error page and everything yourself. Since this extra |
|---|
| | 449 | information will normally be fairly uniform across your site and because you |
|---|
| | 450 | often want to bail out of the middle of a view with a quick "content not |
|---|
| | 451 | found" error, Django provides the ``Http404`` exception. This exception is |
|---|
| | 452 | caught by Django and results in the standard error page for your application |
|---|
| | 453 | being returned along with a 404 error code (although this behavior can be |
|---|
| | 454 | customised, as described below). |
|---|
| | 455 | |
|---|
| | 456 | Using 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 | |
|---|
| | 467 | In order to use the ``Http404`` exception to its fullest, you should create a |
|---|
| | 468 | template that is displayed when a 404 error is raised. This template should be |
|---|
| | 469 | called ``404.html`` and located in the top level of your template tree. |
|---|
| | 470 | |
|---|
| | 471 | Customing error views |
|---|
| | 472 | --------------------- |
|---|
| | 473 | |
|---|
| | 474 | The 404 (page not found) view |
|---|
| | 475 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 476 | |
|---|
| | 477 | When you raise the ``Http404`` exception, Django will load a special view |
|---|
| | 478 | devoted 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 |
|---|
| | 480 | the normal URLconf callbacks use. A 404 view itself has nothing special: It's |
|---|
| | 481 | just a normal view. |
|---|
| | 482 | |
|---|
| | 483 | You normally won't have to bother with writing 404 views. By default, URLconfs |
|---|
| | 484 | contain the following line:: |
|---|
| | 485 | |
|---|
| | 486 | from django.conf.urls.defaults import * |
|---|
| | 487 | |
|---|
| | 488 | That takes care of setting ``handler404`` in the current module. As you can see |
|---|
| | 489 | in ``django/conf/urls/defaults.py``, ``handler404`` is set to |
|---|
| | 490 | ``'django.views.defaults.page_not_found'`` by default. |
|---|
| | 491 | |
|---|
| | 492 | Three 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 | |
|---|
| | 503 | The 500 (server error) view |
|---|
| | 504 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 505 | |
|---|
| | 506 | URLconfs may also define a ``handler500``, which points to a view to call in |
|---|
| | 507 | case of server errors. Server errors happen when you have runtime errors in |
|---|
| | 508 | view code. |
|---|