mirror of https://github.com/django/django.git
Fixed #16214 -- Enhanced documentation about HTTP 404 and 500 status views and templates. Thanks Aymeric Augustin for the report and patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16449 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d257ab4671
commit
a3117699a5
|
@ -134,44 +134,36 @@ to handling 404 errors. By default, it's the view
|
||||||
``404.html``.
|
``404.html``.
|
||||||
|
|
||||||
This means you need to define a ``404.html`` template in your root template
|
This means you need to define a ``404.html`` template in your root template
|
||||||
directory. This template will be used for all 404 errors.
|
directory. This template will be used for all 404 errors. The default 404 view
|
||||||
|
will pass one variable to the template: ``request_path``, which is the URL
|
||||||
|
that resulted in the error.
|
||||||
|
|
||||||
This ``page_not_found`` view should suffice for 99% of Web applications, but if
|
The ``page_not_found`` view should suffice for 99% of Web applications, but if
|
||||||
you want to override the 404 view, you can specify ``handler404`` in your
|
you want to override it, you can specify ``handler404`` in your URLconf, like
|
||||||
URLconf, like so::
|
so::
|
||||||
|
|
||||||
handler404 = 'mysite.views.my_custom_404_view'
|
handler404 = 'mysite.views.my_custom_404_view'
|
||||||
|
|
||||||
Behind the scenes, Django determines the 404 view by looking for ``handler404``.
|
Behind the scenes, Django determines the 404 view by looking for
|
||||||
By default, URLconfs contain the following line::
|
``handler404`` in your root URLconf, and falling back to
|
||||||
|
``django.views.defaults.page_not_found`` if you did not define one.
|
||||||
|
|
||||||
from django.conf.urls.defaults import *
|
Four things to note about 404 views:
|
||||||
|
|
||||||
That takes care of setting ``handler404`` in the current module. As you can see
|
* The 404 view is also called if Django doesn't find a match after
|
||||||
in ``django/conf/urls/defaults.py``, ``handler404`` is set to
|
checking every regular expression in the URLconf.
|
||||||
``'django.views.defaults.page_not_found'`` by default.
|
|
||||||
|
|
||||||
Three things to note about 404 views:
|
* If you don't define your own 404 view — and simply use the default,
|
||||||
|
which is recommended — you still have one obligation: you must create a
|
||||||
* The 404 view is also called if Django doesn't find a match after checking
|
``404.html`` template in the root of your template directory.
|
||||||
every regular expression in the URLconf.
|
|
||||||
|
|
||||||
* If you don't define your own 404 view -- and simply use the
|
|
||||||
default, which is recommended -- you still have one obligation:
|
|
||||||
you must create a ``404.html`` template in the root of your
|
|
||||||
template directory. The default 404 view will use that template
|
|
||||||
for all 404 errors. The default 404 view will pass one variable
|
|
||||||
to the template: ``request_path``, which is the URL that resulted
|
|
||||||
in the 404.
|
|
||||||
|
|
||||||
* The 404 view is passed a :class:`~django.template.RequestContext` and
|
* The 404 view is passed a :class:`~django.template.RequestContext` and
|
||||||
will have access to variables supplied by your
|
will have access to variables supplied by your
|
||||||
:setting:`TEMPLATE_CONTEXT_PROCESSORS` setting (e.g.,
|
:setting:`TEMPLATE_CONTEXT_PROCESSORS` setting (e.g., ``MEDIA_URL``).
|
||||||
:setting:`MEDIA_URL`).
|
|
||||||
|
|
||||||
* If :setting:`DEBUG` is set to ``True`` (in your settings module), then
|
* If :setting:`DEBUG` is set to ``True`` (in your settings module), then
|
||||||
your 404 view will never be used, and the traceback will be displayed
|
your 404 view will never be used, and your URLconf will be displayed
|
||||||
instead.
|
instead, with some debug information.
|
||||||
|
|
||||||
The 500 (server error) view
|
The 500 (server error) view
|
||||||
----------------------------
|
----------------------------
|
||||||
|
@ -187,16 +179,21 @@ view passes no variables to this template and is rendered with an empty
|
||||||
``Context`` to lessen the chance of additional errors.
|
``Context`` to lessen the chance of additional errors.
|
||||||
|
|
||||||
This ``server_error`` view should suffice for 99% of Web applications, but if
|
This ``server_error`` view should suffice for 99% of Web applications, but if
|
||||||
you want to override the view, you can specify ``handler500`` in your
|
you want to override the view, you can specify ``handler500`` in your URLconf,
|
||||||
URLconf, like so::
|
like so::
|
||||||
|
|
||||||
handler500 = 'mysite.views.my_custom_error_view'
|
handler500 = 'mysite.views.my_custom_error_view'
|
||||||
|
|
||||||
Behind the scenes, Django determines the error view by looking for ``handler500``.
|
Behind the scenes, Django determines the 500 view by looking for
|
||||||
By default, URLconfs contain the following line::
|
``handler500`` in your root URLconf, and falling back to
|
||||||
|
``django.views.defaults.server_error`` if you did not define one.
|
||||||
|
|
||||||
from django.conf.urls.defaults import *
|
Two things to note about 500 views:
|
||||||
|
|
||||||
That takes care of setting ``handler500`` in the current module. As you can see
|
* If you don't define your own 500 view — and simply use the default,
|
||||||
in ``django/conf/urls/defaults.py``, ``handler500`` is set to
|
which is recommended — you still have one obligation: you must create a
|
||||||
``'django.views.defaults.server_error'`` by default.
|
``500.html`` template in the root of your template directory.
|
||||||
|
|
||||||
|
* If :setting:`DEBUG` is set to ``True`` (in your settings module), then
|
||||||
|
your 500 view will never be used, and the traceback will be displayed
|
||||||
|
instead, with some debug information.
|
||||||
|
|
Loading…
Reference in New Issue