| 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. |
|---|
| | 441 | ``HttpResponseServerError``, etc., subclasses; just return an instance of one |
|---|
| | 442 | of those subclasses instead of a normal ``HttpResponse`` in order to signify |
|---|
| | 443 | an error. For example:: |
|---|
| | 444 | |
|---|
| | 445 | def my_view(request): |
|---|
| | 446 | # ... |
|---|
| | 447 | if foo: |
|---|
| | 448 | return HttpResponseNotFound('<h1>Page not found</h1>') |
|---|
| | 449 | else: |
|---|
| | 450 | return HttpResponse('<h1>Page was found</h1>') |
|---|
| | 451 | |
|---|
| | 452 | Because 404 errors are by far the most common HTTP error, there's an easier way |
|---|
| | 453 | to handle those errors. |
|---|
| 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 | When you return an error such as ``HttpResponseNotFound``, you're responsible |
|---|
| | 459 | for defining the HTML of the resulting error page:: |
|---|
| | 460 | |
|---|
| | 461 | return HttpResponseNotFound('<h1>Page not found</h1>') |
|---|
| | 462 | |
|---|
| | 463 | For convenience, and because it's a good idea to have a consistent 404 error page |
|---|
| | 464 | across your site, Django provides an ``Http404`` exception. If you raise |
|---|
| | 465 | ``Http404`` at any point in a view function, Django will catch it and return the |
|---|
| | 466 | standard error page for your application, along with an HTTP error code 404. |
|---|
| | 467 | |
|---|
| | 468 | Example usage:: |
|---|
| 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:: |
|---|
| | 487 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 488 | |
|---|
| | 489 | When you raise an ``Http404`` exception, Django loads a special view devoted |
|---|
| | 490 | to handling 404 errors. By default, it's the view |
|---|
| | 491 | ``django.views.defaults.page_not_found``, which loads and renders the template |
|---|
| | 492 | ``404.html``. |
|---|
| | 493 | |
|---|
| | 494 | This means you need to define a ``404.html`` template in your root template |
|---|
| | 495 | directory. This template will be used for all 404 errors. |
|---|
| | 496 | |
|---|
| | 497 | This ``page_not_found`` view should suffice for 99% of Web applications, but if |
|---|
| | 498 | you want to override the 404 view, you can specify ``handler404`` in your |
|---|
| | 499 | URLconf, like so:: |
|---|
| | 500 | |
|---|
| | 501 | handler404 = 'mysite.views.my_custom_404_view' |
|---|
| | 502 | |
|---|
| | 503 | Behind the scenes, Django determines the 404 view by looking for ``handler404``. |
|---|
| | 504 | By default, URLconfs contain the following line:: |
|---|
| 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. |
|---|
| | 526 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 527 | |
|---|
| | 528 | Similarly, Django executes special-case behavior in the case of runtime errors |
|---|
| | 529 | in view code. If a view results in an exception, Django will, by default, call |
|---|
| | 530 | the view ``django.views.defaults.server_error``, which loads and renders the |
|---|
| | 531 | template ``500.html``. |
|---|
| | 532 | |
|---|
| | 533 | This means you need to define a ``500.html`` template in your root template |
|---|
| | 534 | directory. This template will be used for all server errors. |
|---|
| | 535 | |
|---|
| | 536 | This ``server_error`` view should suffice for 99% of Web applications, but if |
|---|
| | 537 | you want to override the view, you can specify ``handler500`` in your |
|---|
| | 538 | URLconf, like so:: |
|---|
| | 539 | |
|---|
| | 540 | handler500 = 'mysite.views.my_custom_error_view' |
|---|
| | 541 | |
|---|
| | 542 | Behind the scenes, Django determines the error view by looking for ``handler500``. |
|---|
| | 543 | By default, URLconfs contain the following line:: |
|---|
| | 544 | |
|---|
| | 545 | from django.conf.urls.defaults import * |
|---|
| | 546 | |
|---|
| | 547 | That takes care of setting ``handler500`` in the current module. As you can see |
|---|
| | 548 | in ``django/conf/urls/defaults.py``, ``handler500`` is set to |
|---|
| | 549 | ``'django.views.defaults.server_error'`` by default. |
|---|