Code

Ticket #2906: request_response.diff

File request_response.diff, 2.9 KB (added by Marc Fargas <telenieko@…>, 7 years ago)

docs.

Line 
1Index: docs/request_response.txt
2===================================================================
3--- docs/request_response.txt   (revisión: 4386)
4+++ docs/request_response.txt   (copia de trabajo)
5@@ -432,3 +432,69 @@
6 
7 ``HttpResponseServerError``
8     Acts just like ``HttpResponse`` but uses a 500 status code.
9+
10+Returning Errors
11+================
12+
13+Returning error codes in Django is easy, as you saw some lines above there
14+are the ``HttpResponseNotFound``, ``HttpResponseForbidden``,
15+``HttpResponseServerError``, etc. subclasses which, when returned by a view,
16+will make the Web server return the corresponding error codes (404, 403, 500,
17+...)
18+
19+But there are more things you can do when returning erros, like calling a
20+personalized view, returning a custom template, ...
21+
22+The Http404 exception
23+---------------------
24+There is an exception in Django that you can raise in your views, it's does
25+the same thing as returning a ``HttpResponseNotFound``, but from the world of
26+exceptions.
27+
28+    from django.http import Http404
29+    # ...
30+    def detail(request, poll_id):
31+        try:
32+            p = Poll.objects.get(pk=poll_id)
33+        except Poll.DoesNotExist:
34+            raise Http404
35+        return render_to_response('polls/detail.html', {'poll': p})
36+
37+Error views
38+~~~~~~~~~~~
39+
40+The 404 (page not found) view
41+-----------------------------
42+
43+When you raise ``Http404`` from within a view, Django will load a special view
44+devoted to handling 404 errors. It finds it by looking for the variable
45+``handler404``, which is a string in Python dotted syntax -- the same format
46+the normal URLconf callbacks use. A 404 view itself has nothing special: It's
47+just a normal view.
48+
49+You normally won't have to bother with writing 404 views. By default, URLconfs
50+have the following line up top::
51+
52+    from django.conf.urls.defaults import *
53+
54+That takes care of setting ``handler404`` in the current module. As you can see
55+in ``django/conf/urls/defaults.py``, ``handler404`` is set to
56+``'django.views.defaults.page_not_found'`` by default.
57+
58+Three more things to note about 404 views:
59+
60+    * The 404 view is also called if Django doesn't find a match after checking
61+      every regular expression in the URLconf.
62+    * If you don't define your own 404 view -- and simply use the default,
63+      which is recommended -- you still have one obligation: To create a
64+      ``404.html`` template in the root of your template directory. The default
65+      404 view will use that template for all 404 errors.
66+    * If ``DEBUG`` is set to ``True`` (in your settings module) then your 404
67+      view will never be used, and the traceback will be displayed instead.
68+
69+The 500 (server error) view
70+---------------------------
71+
72+Similarly, URLconfs may define a ``handler500``, which points to a view to call
73+in case of server errors. Server errors happen when you have runtime errors in
74+view code.