2007-09-14 23:09:38 +02:00
|
|
|
=========================
|
|
|
|
Django shortcut functions
|
|
|
|
=========================
|
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
.. module:: django.shortcuts
|
|
|
|
:synopsis:
|
2012-11-02 09:29:55 +01:00
|
|
|
Convenience shortcuts that span multiple levels of Django's MVC stack.
|
2009-03-21 14:09:13 +01:00
|
|
|
|
|
|
|
.. index:: shortcuts
|
|
|
|
|
2007-09-14 23:09:38 +02:00
|
|
|
The package ``django.shortcuts`` collects helper functions and classes that
|
|
|
|
"span" multiple levels of MVC. In other words, these functions/classes
|
|
|
|
introduce controlled coupling for convenience's sake.
|
|
|
|
|
2010-12-21 18:18:41 +01:00
|
|
|
``render``
|
|
|
|
==========
|
|
|
|
|
2012-06-21 01:13:35 +02:00
|
|
|
.. function:: render(request, template_name[, dictionary][, context_instance][, content_type][, status][, current_app])
|
2010-12-21 18:18:41 +01:00
|
|
|
|
|
|
|
Combines a given template with a given context dictionary and returns an
|
|
|
|
:class:`~django.http.HttpResponse` object with that rendered text.
|
|
|
|
|
|
|
|
:func:`render()` is the same as a call to
|
2011-01-05 23:41:43 +01:00
|
|
|
:func:`render_to_response()` with a `context_instance` argument that
|
2011-03-29 12:24:42 +02:00
|
|
|
forces the use of a :class:`~django.template.RequestContext`.
|
2010-12-21 18:18:41 +01:00
|
|
|
|
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``request``
|
|
|
|
The request object used to generate this response.
|
|
|
|
|
2012-06-21 01:13:35 +02:00
|
|
|
``template_name``
|
2010-12-21 18:18:41 +01:00
|
|
|
The full name of a template to use or sequence of template names.
|
|
|
|
|
|
|
|
Optional arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``dictionary``
|
|
|
|
A dictionary of values to add to the template context. By default, this
|
|
|
|
is an empty dictionary. If a value in the dictionary is callable, the
|
|
|
|
view will call it just before rendering the template.
|
|
|
|
|
|
|
|
``context_instance``
|
|
|
|
The context instance to render the template with. By default, the template
|
|
|
|
will be rendered with a ``RequestContext`` instance (filled with values from
|
2011-03-04 22:13:42 +01:00
|
|
|
``request`` and ``dictionary``).
|
2010-12-21 18:18:41 +01:00
|
|
|
|
2010-12-22 05:54:10 +01:00
|
|
|
``content_type``
|
2010-12-21 18:18:41 +01:00
|
|
|
The MIME type to use for the resulting document. Defaults to the value of
|
|
|
|
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
|
|
|
|
2013-01-31 13:39:29 +01:00
|
|
|
.. versionchanged:: 1.5
|
|
|
|
This parameter used to be called ``mimetype``.
|
|
|
|
|
2010-12-22 05:54:10 +01:00
|
|
|
``status``
|
|
|
|
The status code for the response. Defaults to ``200``.
|
|
|
|
|
2011-01-05 23:41:43 +01:00
|
|
|
``current_app``
|
|
|
|
A hint indicating which application contains the current view. See the
|
|
|
|
:ref:`namespaced URL resolution strategy <topics-http-reversing-url-namespaces>`
|
|
|
|
for more information.
|
|
|
|
|
2010-12-21 18:18:41 +01:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example renders the template ``myapp/index.html`` with the
|
2011-09-04 23:17:30 +02:00
|
|
|
MIME type :mimetype:`application/xhtml+xml`::
|
2010-12-21 18:18:41 +01:00
|
|
|
|
2010-12-22 05:54:10 +01:00
|
|
|
from django.shortcuts import render
|
2010-12-21 18:18:41 +01:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
2010-12-22 05:54:10 +01:00
|
|
|
return render(request, 'myapp/index.html', {"foo": "bar"},
|
|
|
|
content_type="application/xhtml+xml")
|
2010-12-21 18:18:41 +01:00
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
2011-06-26 18:52:50 +02:00
|
|
|
from django.template import RequestContext, loader
|
2010-12-21 18:18:41 +01:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
|
|
|
t = loader.get_template('myapp/template.html')
|
|
|
|
c = RequestContext(request, {'foo': 'bar'})
|
|
|
|
return HttpResponse(t.render(c),
|
2010-12-22 05:54:10 +01:00
|
|
|
content_type="application/xhtml+xml")
|
2010-12-21 18:18:41 +01:00
|
|
|
|
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
``render_to_response``
|
|
|
|
======================
|
|
|
|
|
2013-01-31 13:39:29 +01:00
|
|
|
.. function:: render_to_response(template_name[, dictionary][, context_instance][, content_type])
|
2007-09-14 23:09:38 +02:00
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
Renders a given template with a given context dictionary and returns an
|
|
|
|
:class:`~django.http.HttpResponse` object with that rendered text.
|
2007-09-14 23:09:38 +02:00
|
|
|
|
2007-09-15 00:33:06 +02:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
2011-05-18 22:33:12 +02:00
|
|
|
``template_name``
|
2010-12-04 21:20:54 +01:00
|
|
|
The full name of a template to use or sequence of template names. If a
|
|
|
|
sequence is given, the first template that exists will be used. See the
|
|
|
|
:ref:`template loader documentation <ref-templates-api-the-python-api>`
|
|
|
|
for more information on how templates are found.
|
2007-09-15 00:33:06 +02:00
|
|
|
|
|
|
|
Optional arguments
|
|
|
|
------------------
|
|
|
|
|
2008-01-31 23:49:14 +01:00
|
|
|
``dictionary``
|
2007-09-15 00:33:06 +02:00
|
|
|
A dictionary of values to add to the template context. By default, this
|
|
|
|
is an empty dictionary. If a value in the dictionary is callable, the
|
2008-08-24 00:25:40 +02:00
|
|
|
view will call it just before rendering the template.
|
2007-09-15 00:33:06 +02:00
|
|
|
|
2008-01-31 23:49:14 +01:00
|
|
|
``context_instance``
|
|
|
|
The context instance to render the template with. By default, the template
|
2010-12-05 17:47:39 +01:00
|
|
|
will be rendered with a :class:`~django.template.Context` instance (filled
|
|
|
|
with values from ``dictionary``). If you need to use :ref:`context
|
|
|
|
processors <subclassing-context-requestcontext>`, render the template with
|
|
|
|
a :class:`~django.template.RequestContext` instance instead. Your code
|
|
|
|
might look something like this::
|
2008-01-31 23:49:14 +01:00
|
|
|
|
|
|
|
return render_to_response('my_template.html',
|
|
|
|
my_data_dictionary,
|
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
2013-01-31 13:39:29 +01:00
|
|
|
``content_type``
|
2008-09-02 05:40:42 +02:00
|
|
|
The MIME type to use for the resulting document. Defaults to the value of
|
|
|
|
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
2007-09-15 00:33:06 +02:00
|
|
|
|
2013-01-31 13:39:29 +01:00
|
|
|
.. versionchanged:: 1.5
|
|
|
|
This parameter used to be called ``mimetype``.
|
|
|
|
|
|
|
|
|
2007-09-15 00:33:06 +02:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example renders the template ``myapp/index.html`` with the
|
2011-09-04 23:17:30 +02:00
|
|
|
MIME type :mimetype:`application/xhtml+xml`::
|
2007-09-14 23:09:38 +02:00
|
|
|
|
|
|
|
from django.shortcuts import render_to_response
|
2007-09-15 00:33:06 +02:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
|
|
|
return render_to_response('myapp/index.html', {"foo": "bar"},
|
|
|
|
mimetype="application/xhtml+xml")
|
2007-09-14 23:09:38 +02:00
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template import Context, loader
|
2007-09-15 00:33:06 +02:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
|
|
|
t = loader.get_template('myapp/template.html')
|
|
|
|
c = Context({'foo': 'bar'})
|
2009-03-31 18:49:54 +02:00
|
|
|
return HttpResponse(t.render(c),
|
2013-01-31 13:39:29 +01:00
|
|
|
content_type="application/xhtml+xml")
|
2007-09-15 00:33:06 +02:00
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
``redirect``
|
|
|
|
============
|
|
|
|
|
|
|
|
.. function:: redirect(to[, permanent=False], *args, **kwargs)
|
|
|
|
|
2010-12-05 17:47:39 +01:00
|
|
|
Returns an :class:`~django.http.HttpResponseRedirect` to the appropriate URL
|
|
|
|
for the arguments passed.
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
The arguments could be:
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* A model: the model's `get_absolute_url()` function will be called.
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2013-03-22 10:50:45 +01:00
|
|
|
* A view name, possibly with arguments: :func:`urlresolvers.reverse
|
|
|
|
<django.core.urlresolvers.reverse>` will be used to reverse-resolve the
|
|
|
|
name.
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
* A URL, which will be used as-is for the redirect location.
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2010-12-05 17:47:39 +01:00
|
|
|
By default issues a temporary redirect; pass ``permanent=True`` to issue a
|
2009-03-21 14:09:13 +01:00
|
|
|
permanent redirect
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
|
|
|
You can use the :func:`redirect` function in a number of ways.
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
1. By passing some object; that object's
|
|
|
|
:meth:`~django.db.models.Model.get_absolute_url` method will be called
|
|
|
|
to figure out the redirect URL::
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2012-04-22 17:58:53 +02:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
object = MyModel.objects.get(...)
|
|
|
|
return redirect(object)
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
2. By passing the name of a view and optionally some positional or
|
|
|
|
keyword arguments; the URL will be reverse resolved using the
|
|
|
|
:func:`~django.core.urlresolvers.reverse` method::
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('some-view-name', foo='bar')
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
3. By passing a hardcoded URL to redirect to::
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('/some/url/')
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
This also works with full URLs::
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2011-10-14 02:12:01 +02:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('http://example.com/')
|
2009-03-21 16:26:56 +01:00
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
By default, :func:`redirect` returns a temporary redirect. All of the above
|
|
|
|
forms accept a ``permanent`` argument; if set to ``True`` a permanent redirect
|
|
|
|
will be returned::
|
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
object = MyModel.objects.get(...)
|
|
|
|
return redirect(object, permanent=True)
|
|
|
|
|
2007-09-14 23:09:38 +02:00
|
|
|
``get_object_or_404``
|
|
|
|
=====================
|
|
|
|
|
2009-09-13 00:41:12 +02:00
|
|
|
.. function:: get_object_or_404(klass, *args, **kwargs)
|
2009-03-21 14:09:13 +01:00
|
|
|
|
2011-03-08 20:51:19 +01:00
|
|
|
Calls :meth:`~django.db.models.query.QuerySet.get()` on a given model manager,
|
2010-12-05 17:47:39 +01:00
|
|
|
but it raises :class:`~django.http.Http404` instead of the model's
|
|
|
|
:class:`~django.core.exceptions.DoesNotExist` exception.
|
2007-09-14 23:09:38 +02:00
|
|
|
|
2007-09-15 00:33:06 +02:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``klass``
|
2010-12-05 17:47:39 +01:00
|
|
|
A :class:`~django.db.models.Model`, :class:`~django.db.models.Manager` or
|
2011-03-08 20:51:19 +01:00
|
|
|
:class:`~django.db.models.query.QuerySet` instance from which to get the
|
|
|
|
object.
|
2007-09-15 00:33:06 +02:00
|
|
|
|
|
|
|
``**kwargs``
|
|
|
|
Lookup parameters, which should be in the format accepted by ``get()`` and
|
|
|
|
``filter()``.
|
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example gets the object with the primary key of 1 from
|
|
|
|
``MyModel``::
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
my_object = get_object_or_404(MyModel, pk=1)
|
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import Http404
|
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
try:
|
2008-01-09 08:19:56 +01:00
|
|
|
my_object = MyModel.objects.get(pk=1)
|
2007-09-15 00:33:06 +02:00
|
|
|
except MyModel.DoesNotExist:
|
|
|
|
raise Http404
|
|
|
|
|
2010-12-05 17:47:39 +01:00
|
|
|
Note: As with ``get()``, a
|
|
|
|
:class:`~django.core.exceptions.MultipleObjectsReturned` exception
|
|
|
|
will be raised if more than one object is found.
|
2007-09-15 00:33:06 +02:00
|
|
|
|
2007-09-14 23:09:38 +02:00
|
|
|
``get_list_or_404``
|
|
|
|
===================
|
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
.. function:: get_list_or_404(klass, *args, **kwargs)
|
|
|
|
|
2011-03-08 20:51:19 +01:00
|
|
|
Returns the result of :meth:`~django.db.models.query.QuerySet.filter()` on a
|
2010-12-05 17:47:39 +01:00
|
|
|
given model manager, raising :class:`~django.http.Http404` if the resulting
|
|
|
|
list is empty.
|
2007-09-15 00:33:06 +02:00
|
|
|
|
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``klass``
|
2010-12-05 17:47:39 +01:00
|
|
|
A :class:`~django.db.models.Model`, :class:`~django.db.models.Manager` or
|
|
|
|
:class:`~django.db.models.query.QuerySet` instance from which to get the
|
|
|
|
list.
|
2007-09-15 00:33:06 +02:00
|
|
|
|
|
|
|
``**kwargs``
|
|
|
|
Lookup parameters, which should be in the format accepted by ``get()`` and
|
|
|
|
``filter()``.
|
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example gets all published objects from ``MyModel``::
|
|
|
|
|
|
|
|
from django.shortcuts import get_list_or_404
|
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
my_objects = get_list_or_404(MyModel, published=True)
|
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import Http404
|
|
|
|
|
|
|
|
def my_view(request):
|
2009-02-22 07:06:56 +01:00
|
|
|
my_objects = list(MyModel.objects.filter(published=True))
|
2007-09-15 00:33:06 +02:00
|
|
|
if not my_objects:
|
2008-09-02 05:40:42 +02:00
|
|
|
raise Http404
|