Fix a small typo in docs/upload_handling.txt
[django.git] / docs / shortcuts.txt
blob005fdc3029f3cf5c6773a911262ddf1e2bdadb90
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 ``dictionary``
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 ``context_instance``
31     The context instance to render the template with. By default, the template
32     will be rendered with a ``Context`` instance (filled with values from
33     ``dictionary``). If you need to use `context processors`_, render the
34     template with a ``RequestContext`` instance instead. Your code might look
35     something like this::
37         return render_to_response('my_template.html',
38                                   my_data_dictionary,
39                                   context_instance=RequestContext(request))
41 ``mimetype``
42     **New in Django development version:** The MIME type to use for the
43     resulting document. Defaults to the value of the ``DEFAULT_CONTENT_TYPE``
44     setting.
46 .. _`context processors`: ../templates_python/#subclassing-context-requestcontext
48 Example
49 -------
51 The following example renders the template ``myapp/index.html`` with the
52 MIME type ``application/xhtml+xml``::
54     from django.shortcuts import render_to_response
56     def my_view(request):
57         # View code here...
58         return render_to_response('myapp/index.html', {"foo": "bar"},
59             mimetype="application/xhtml+xml")
61 This example is equivalent to::
63     from django.http import HttpResponse
64     from django.template import Context, loader
66     def my_view(request):
67         # View code here...
68         t = loader.get_template('myapp/template.html')
69         c = Context({'foo': 'bar'})
70         r = HttpResponse(t.render(c),
71             mimetype="application/xhtml+xml")
73 ``get_object_or_404``
74 =====================
76 ``django.shortcuts.get_object_or_404`` calls `get()`_ on a given model
77 manager, but it raises ``django.http.Http404`` instead of the model's
78 ``DoesNotExist`` exception.
80 Required arguments
81 ------------------
83 ``klass``
84     A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the
85     object.
87 ``**kwargs``
88     Lookup parameters, which should be in the format accepted by ``get()`` and
89     ``filter()``.
91 Example
92 -------
94 The following example gets the object with the primary key of 1 from
95 ``MyModel``::
97     from django.shortcuts import get_object_or_404
99     def my_view(request):
100         my_object = get_object_or_404(MyModel, pk=1)
102 This example is equivalent to::
104     from django.http import Http404
106     def my_view(request):
107         try:
108             my_object = MyModel.objects.get(pk=1)
109         except MyModel.DoesNotExist:
110             raise Http404
112 Note: As with ``get()``, an ``MultipleObjectsReturned`` exception will be
113 raised if more than one object is found.
115 .. _get(): ../db-api/#get-kwargs
117 ``get_list_or_404``
118 ===================
120 ``django.shortcuts.get_list_or_404`` returns the result of `filter()`_ on a
121 given model manager, raising ``django.http.Http404`` if the resulting list is
122 empty.
124 Required arguments
125 ------------------
127 ``klass``
128     A ``Model``, ``Manager`` or ``QuerySet`` instance from which to get the
129     object.
131 ``**kwargs``
132     Lookup parameters, which should be in the format accepted by ``get()`` and
133     ``filter()``.
135 Example
136 -------
138 The following example gets all published objects from ``MyModel``::
140     from django.shortcuts import get_list_or_404
142     def my_view(request):
143         my_objects = get_list_or_404(MyModel, published=True)
145 This example is equivalent to::
147     from django.http import Http404
149     def my_view(request):
150         my_objects = MyModel.objects.filter(published=True)
151         if not my_objects:
152             raise Http404
154 .. _filter(): ../db-api/#filter-kwargs