|  | 435 |  | 
          
            |  | 436 | Returning Errors | 
          
            |  | 437 | ================ | 
          
            |  | 438 |  | 
          
            |  | 439 | Returning error codes in Django is easy, as you saw some lines above there | 
          
            |  | 440 | are the ``HttpResponseNotFound``, ``HttpResponseForbidden``, | 
          
            |  | 441 | ``HttpResponseServerError``, etc. subclasses which, when returned by a view, | 
          
            |  | 442 | will make the Web server return the corresponding error codes (404, 403, 500, | 
          
            |  | 443 | ...) | 
          
            |  | 444 |  | 
          
            |  | 445 | But there are more things you can do when returning erros, like calling a | 
          
            |  | 446 | personalized view, returning a custom template, ... | 
          
            |  | 447 |  | 
          
            |  | 448 | The Http404 exception | 
          
            |  | 449 | --------------------- | 
          
            |  | 450 | There is an exception in Django that you can raise in your views, it's does | 
          
            |  | 451 | the same thing as returning a ``HttpResponseNotFound``, but from the world of | 
          
            |  | 452 | exceptions. | 
          
            |  | 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 |  | 
          
            |  | 463 | Error views | 
          
            |  | 464 | ~~~~~~~~~~~ | 
          
            |  | 465 |  | 
          
            |  | 466 | The 404 (page not found) view | 
          
            |  | 467 | ----------------------------- | 
          
            |  | 468 |  | 
          
            |  | 469 | When you raise ``Http404`` from within a view, Django will load a special view | 
          
            |  | 470 | devoted 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 | 
          
            |  | 472 | the normal URLconf callbacks use. A 404 view itself has nothing special: It's | 
          
            |  | 473 | just a normal view. | 
          
            |  | 474 |  | 
          
            |  | 475 | You normally won't have to bother with writing 404 views. By default, URLconfs | 
          
            |  | 476 | have the following line up top:: | 
          
            |  | 477 |  | 
          
            |  | 478 | from django.conf.urls.defaults import * | 
          
            |  | 479 |  | 
          
            |  | 480 | That takes care of setting ``handler404`` in the current module. As you can see | 
          
            |  | 481 | in ``django/conf/urls/defaults.py``, ``handler404`` is set to | 
          
            |  | 482 | ``'django.views.defaults.page_not_found'`` by default. | 
          
            |  | 483 |  | 
          
            |  | 484 | Three 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 |  | 
          
            |  | 495 | The 500 (server error) view | 
          
            |  | 496 | --------------------------- | 
          
            |  | 497 |  | 
          
            |  | 498 | Similarly, URLconfs may define a ``handler500``, which points to a view to call | 
          
            |  | 499 | in case of server errors. Server errors happen when you have runtime errors in | 
          
            |  | 500 | view code. |