Fixed #5550: Documented the context used by the default view for 404 and 500 errors.
[django.git] / docs / shortcuts.txt
blob6c55486b5fbf01172ba45bf866ac34c239868f21
1 =========================
2 Django shortcut functions
3 =========================
5 The package ``django.shortcuts`` collects helper functions and classes that
6 "span" multiple levels of MVC. In other words, these functions/classes
7 introduce controlled coupling for convenience's sake.
9 ``render_to_response()``
10 ========================
12 ``django.shortcuts.render_to_response`` renders a given template with a given
13 context dictionary and returns an ``HttpResponse`` object with that rendered
14 text.
16 Required arguments
17 ------------------
19 ``template``
20     The full name of a template to use.
22 Optional arguments
23 ------------------
25 ``context``
26     A dictionary of values to add to the template context. By default, this
27     is an empty dictionary. If a value in the dictionary is callable, the
28     view will call it just before rendering  the template.
30 ``mimetype``
31     **New in Django development version:** The MIME type to use for the
32     resulting document. Defaults to the value of the ``DEFAULT_CONTENT_TYPE``
33     setting.
35 Example
36 -------
38 The following example renders the template ``myapp/index.html`` with the
39 MIME type ``application/xhtml+xml``::
41     from django.shortcuts import render_to_response
43     def my_view(request):
44         # View code here...
45         return render_to_response('myapp/index.html', {"foo": "bar"},
46             mimetype="application/xhtml+xml")
48 This example is equivalent to::
50     from django.http import HttpResponse
51     from django.template import Context, loader
53     def my_view(request):
54         # View code here...
55         t = loader.get_template('myapp/template.html')
56         c = Context({'foo': 'bar'})
57         r = HttpResponse(t.render(c),
58             mimetype="application/xhtml+xml")
60 .. _an HttpResponse object: ../request_response/#httpresponse-objects
62 ``get_object_or_404``
63 =====================
65 ``django.shortcuts.get_object_or_404`` calls `get()`_ on a given model
66 manager, but it raises ``django.http.Http404`` instead of the model's
67 ``DoesNotExist`` exception.
69 Required arguments
70 ------------------
72 ``klass``
73     A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the
74     object.
76 ``**kwargs``
77     Lookup parameters, which should be in the format accepted by ``get()`` and
78     ``filter()``.
80 Example
81 -------
83 The following example gets the object with the primary key of 1 from
84 ``MyModel``::
86     from django.shortcuts import get_object_or_404
88     def my_view(request):
89         my_object = get_object_or_404(MyModel, pk=1)
91 This example is equivalent to::
93     from django.http import Http404
95     def my_view(request):
96         try:
97             my_object = MyModel.object.get(pk=1)
98         except MyModel.DoesNotExist:
99             raise Http404
101 Note: As with ``get()``, an ``AssertionError`` will be raised if more than
102 one object is found.
104 .. _get(): ../db-api/#get-kwargs
106 ``get_list_or_404``
107 ===================
109 ``django.shortcuts.get_list_or_404`` returns the result of `filter()`_ on a
110 given model manager, raising ``django.http.Http404`` if the resulting list is
111 empty.
113 Required arguments
114 ------------------
116 ``klass``
117     A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the
118     object.
120 ``**kwargs``
121     Lookup parameters, which should be in the format accepted by ``get()`` and
122     ``filter()``.
124 Example
125 -------
127 The following example gets all published objects from ``MyModel``::
129     from django.shortcuts import get_list_or_404
131     def my_view(request):
132         my_objects = get_list_or_404(MyModel, published=True)
134 This example is equivalent to::
136     from django.http import Http404
138     def my_view(request):
139         my_objects = MyModels.object.filter(published=True)
140         if not my_objects:
141             raise Http404
143 .. _filter(): ../db-api/#filter-kwargs