2007-09-14 21:09:38 +00:00
|
|
|
=========================
|
|
|
|
Django shortcut functions
|
|
|
|
=========================
|
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
.. module:: django.shortcuts
|
|
|
|
:synopsis:
|
2012-11-02 08:29:55 +00:00
|
|
|
Convenience shortcuts that span multiple levels of Django's MVC stack.
|
2009-03-21 13:09:13 +00:00
|
|
|
|
|
|
|
.. index:: shortcuts
|
|
|
|
|
2007-09-14 21:09:38 +00: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 17:18:41 +00:00
|
|
|
``render``
|
|
|
|
==========
|
|
|
|
|
2015-01-26 20:57:10 +00:00
|
|
|
.. function:: render(request, template_name[, context][, context_instance][, content_type][, status][, current_app][, dirs][, using])
|
2010-12-21 17:18:41 +00: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
|
2013-08-15 11:14:10 +00:00
|
|
|
:func:`render_to_response()` with a ``context_instance`` argument that
|
2011-03-29 10:24:42 +00:00
|
|
|
forces the use of a :class:`~django.template.RequestContext`.
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2013-12-10 23:24:31 +00:00
|
|
|
Django does not provide a shortcut function which returns a
|
|
|
|
:class:`~django.template.response.TemplateResponse` because the constructor
|
|
|
|
of :class:`~django.template.response.TemplateResponse` offers the same level
|
|
|
|
of convenience as :func:`render()`.
|
|
|
|
|
2010-12-21 17:18:41 +00:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``request``
|
|
|
|
The request object used to generate this response.
|
|
|
|
|
2012-06-20 23:13:35 +00:00
|
|
|
``template_name``
|
2010-12-21 17:18:41 +00:00
|
|
|
The full name of a template to use or sequence of template names.
|
|
|
|
|
|
|
|
Optional arguments
|
|
|
|
------------------
|
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
``context``
|
2010-12-21 17:18:41 +00: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
|
|
|
|
view will call it just before rendering the template.
|
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
.. versionchanged:: 1.8
|
|
|
|
|
|
|
|
The ``context`` argument used to be called ``dictionary``. That name
|
|
|
|
is deprecated in Django 1.8 and will be removed in Django 2.0.
|
|
|
|
|
2010-12-21 17:18:41 +00:00
|
|
|
``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
|
2014-12-14 09:17:18 +00:00
|
|
|
``request`` and ``context``).
|
|
|
|
|
|
|
|
.. deprecated:: 1.8
|
|
|
|
|
|
|
|
The ``context_instance`` argument is deprecated. Simply use ``context``.
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2010-12-22 04:54:10 +00:00
|
|
|
``content_type``
|
2010-12-21 17:18:41 +00:00
|
|
|
The MIME type to use for the resulting document. Defaults to the value of
|
|
|
|
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
|
|
|
|
2010-12-22 04:54:10 +00:00
|
|
|
``status``
|
|
|
|
The status code for the response. Defaults to ``200``.
|
|
|
|
|
2011-01-05 22:41:43 +00: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.
|
|
|
|
|
2014-12-14 16:48:51 +00:00
|
|
|
.. deprecated:: 1.8
|
|
|
|
|
|
|
|
The ``current_app`` argument is deprecated. Instead you should set
|
|
|
|
``request.current_app``.
|
|
|
|
|
2015-01-26 20:57:10 +00:00
|
|
|
``using``
|
|
|
|
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
|
|
|
loading the template.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.8
|
|
|
|
|
|
|
|
The ``using`` parameter was added.
|
|
|
|
|
2013-09-15 21:51:24 +00:00
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
The ``dirs`` parameter was added.
|
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
.. deprecated:: 1.8
|
2014-11-13 19:25:08 +00:00
|
|
|
|
|
|
|
The ``dirs`` parameter was deprecated.
|
|
|
|
|
2010-12-21 17:18:41 +00:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example renders the template ``myapp/index.html`` with the
|
2011-09-04 21:17:30 +00:00
|
|
|
MIME type :mimetype:`application/xhtml+xml`::
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2010-12-22 04:54:10 +00:00
|
|
|
from django.shortcuts import render
|
2010-12-21 17:18:41 +00:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
2010-12-22 04:54:10 +00:00
|
|
|
return render(request, 'myapp/index.html', {"foo": "bar"},
|
|
|
|
content_type="application/xhtml+xml")
|
2010-12-21 17:18:41 +00:00
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
2011-06-26 16:52:50 +00:00
|
|
|
from django.template import RequestContext, loader
|
2010-12-21 17:18:41 +00:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
2013-11-15 12:09:46 +00:00
|
|
|
t = loader.get_template('myapp/index.html')
|
2010-12-21 17:18:41 +00:00
|
|
|
c = RequestContext(request, {'foo': 'bar'})
|
|
|
|
return HttpResponse(t.render(c),
|
2010-12-22 04:54:10 +00:00
|
|
|
content_type="application/xhtml+xml")
|
2010-12-21 17:18:41 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
``render_to_response``
|
|
|
|
======================
|
|
|
|
|
2015-01-26 20:57:10 +00:00
|
|
|
.. function:: render_to_response(template_name[, context][, context_instance][, content_type][, status][, dirs][, using])
|
2007-09-14 21:09:38 +00:00
|
|
|
|
2009-03-21 13:09:13 +00: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 21:09:38 +00:00
|
|
|
|
2007-09-14 22:33:06 +00:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
2011-05-18 20:33:12 +00:00
|
|
|
``template_name``
|
2010-12-04 20:20:54 +00: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
|
2015-01-03 22:05:34 +00:00
|
|
|
:ref:`template loading documentation <template-loading>` for more
|
|
|
|
information on how templates are found.
|
2007-09-14 22:33:06 +00:00
|
|
|
|
|
|
|
Optional arguments
|
|
|
|
------------------
|
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
``context``
|
2007-09-14 22:33:06 +00: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-23 22:25:40 +00:00
|
|
|
view will call it just before rendering the template.
|
2007-09-14 22:33:06 +00:00
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
.. versionchanged:: 1.8
|
|
|
|
|
|
|
|
The ``context`` argument used to be called ``dictionary``. That name
|
|
|
|
is deprecated in Django 1.8 and will be removed in Django 2.0.
|
|
|
|
|
2008-01-31 22:49:14 +00:00
|
|
|
``context_instance``
|
|
|
|
The context instance to render the template with. By default, the template
|
2010-12-05 16:47:39 +00:00
|
|
|
will be rendered with a :class:`~django.template.Context` instance (filled
|
2014-12-14 09:17:18 +00:00
|
|
|
with values from ``context``). If you need to use :ref:`context
|
2010-12-05 16:47:39 +00:00
|
|
|
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 22:49:14 +00:00
|
|
|
|
|
|
|
return render_to_response('my_template.html',
|
2014-12-14 09:17:18 +00:00
|
|
|
my_context,
|
2008-01-31 22:49:14 +00:00
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
.. deprecated:: 1.8
|
|
|
|
|
|
|
|
The ``context_instance`` argument is deprecated. Simply use ``context``.
|
|
|
|
|
2013-01-31 12:39:29 +00:00
|
|
|
``content_type``
|
2008-09-02 03:40:42 +00:00
|
|
|
The MIME type to use for the resulting document. Defaults to the value of
|
|
|
|
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
2007-09-14 22:33:06 +00:00
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
``status``
|
|
|
|
The status code for the response. Defaults to ``200``.
|
|
|
|
|
2015-01-26 20:57:10 +00:00
|
|
|
``using``
|
|
|
|
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
|
|
|
loading the template.
|
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
.. versionchanged:: 1.8
|
|
|
|
|
2015-01-26 20:57:10 +00:00
|
|
|
The ``status`` and ``using`` parameters were added.
|
2014-12-14 09:17:18 +00:00
|
|
|
|
2013-09-15 21:51:24 +00:00
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
The ``dirs`` parameter was added.
|
|
|
|
|
2014-12-14 09:17:18 +00:00
|
|
|
.. deprecated:: 1.8
|
2014-11-13 19:25:08 +00:00
|
|
|
|
|
|
|
The ``dirs`` parameter was deprecated.
|
|
|
|
|
2007-09-14 22:33:06 +00:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example renders the template ``myapp/index.html`` with the
|
2011-09-04 21:17:30 +00:00
|
|
|
MIME type :mimetype:`application/xhtml+xml`::
|
2007-09-14 21:09:38 +00:00
|
|
|
|
|
|
|
from django.shortcuts import render_to_response
|
2007-09-14 22:33:06 +00:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
|
|
|
return render_to_response('myapp/index.html', {"foo": "bar"},
|
2014-08-07 00:32:41 +00:00
|
|
|
content_type="application/xhtml+xml")
|
2007-09-14 21:09:38 +00:00
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template import Context, loader
|
2007-09-14 22:33:06 +00:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
2013-11-15 15:21:58 +00:00
|
|
|
t = loader.get_template('myapp/index.html')
|
2007-09-14 22:33:06 +00:00
|
|
|
c = Context({'foo': 'bar'})
|
2009-03-31 16:49:54 +00:00
|
|
|
return HttpResponse(t.render(c),
|
2013-01-31 12:39:29 +00:00
|
|
|
content_type="application/xhtml+xml")
|
2007-09-14 22:33:06 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
``redirect``
|
|
|
|
============
|
|
|
|
|
|
|
|
.. function:: redirect(to[, permanent=False], *args, **kwargs)
|
|
|
|
|
2010-12-05 16:47:39 +00:00
|
|
|
Returns an :class:`~django.http.HttpResponseRedirect` to the appropriate URL
|
|
|
|
for the arguments passed.
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
The arguments could be:
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2013-10-14 11:59:56 +00:00
|
|
|
* A model: the model's :meth:`~django.db.models.Model.get_absolute_url()`
|
2013-08-15 11:14:10 +00:00
|
|
|
function will be called.
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2013-03-22 09:50:45 +00: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 15:26:56 +00:00
|
|
|
|
2014-02-14 03:48:44 +00:00
|
|
|
* An absolute or relative URL, which will be used as-is for the redirect
|
|
|
|
location.
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2010-12-05 16:47:39 +00:00
|
|
|
By default issues a temporary redirect; pass ``permanent=True`` to issue a
|
2014-02-14 03:48:44 +00:00
|
|
|
permanent redirect.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
The ability to use relative URLs was added.
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
|
|
|
You can use the :func:`redirect` function in a number of ways.
|
|
|
|
|
2011-10-14 00:12:01 +00: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 15:26:56 +00:00
|
|
|
|
2012-04-22 15:58:53 +00:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
object = MyModel.objects.get(...)
|
|
|
|
return redirect(object)
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2011-10-14 00:12:01 +00: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 15:26:56 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('some-view-name', foo='bar')
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
3. By passing a hardcoded URL to redirect to::
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('/some/url/')
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
This also works with full URLs::
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('http://example.com/')
|
2009-03-21 15:26:56 +00:00
|
|
|
|
2009-03-21 13:09:13 +00: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 21:09:38 +00:00
|
|
|
``get_object_or_404``
|
|
|
|
=====================
|
|
|
|
|
2009-09-12 22:41:12 +00:00
|
|
|
.. function:: get_object_or_404(klass, *args, **kwargs)
|
2009-03-21 13:09:13 +00:00
|
|
|
|
2011-03-08 19:51:19 +00:00
|
|
|
Calls :meth:`~django.db.models.query.QuerySet.get()` on a given model manager,
|
2010-12-05 16:47:39 +00:00
|
|
|
but it raises :class:`~django.http.Http404` instead of the model's
|
|
|
|
:class:`~django.core.exceptions.DoesNotExist` exception.
|
2007-09-14 21:09:38 +00:00
|
|
|
|
2007-09-14 22:33:06 +00:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``klass``
|
2013-11-19 14:25:15 +00:00
|
|
|
A :class:`~django.db.models.Model` class,
|
|
|
|
a :class:`~django.db.models.Manager`,
|
|
|
|
or a :class:`~django.db.models.query.QuerySet` instance from which to get
|
|
|
|
the object.
|
2007-09-14 22:33:06 +00: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 07:19:56 +00:00
|
|
|
my_object = MyModel.objects.get(pk=1)
|
2007-09-14 22:33:06 +00:00
|
|
|
except MyModel.DoesNotExist:
|
2015-01-13 08:02:17 +00:00
|
|
|
raise Http404("No MyModel matches the given query.")
|
2007-09-14 22:33:06 +00:00
|
|
|
|
2013-11-19 14:25:15 +00:00
|
|
|
The most common use case is to pass a :class:`~django.db.models.Model`, as
|
|
|
|
shown above. However, you can also pass a
|
|
|
|
:class:`~django.db.models.query.QuerySet` instance::
|
|
|
|
|
|
|
|
queryset = Book.objects.filter(title__startswith='M')
|
|
|
|
get_object_or_404(queryset, pk=1)
|
|
|
|
|
|
|
|
The above example is a bit contrived since it's equivalent to doing::
|
|
|
|
|
|
|
|
get_object_or_404(Book, title__startswith='M', pk=1)
|
|
|
|
|
|
|
|
but it can be useful if you are passed the ``queryset`` variable from somewhere
|
|
|
|
else.
|
|
|
|
|
|
|
|
Finally, you can also use a :class:`~django.db.models.Manager`. This is useful
|
|
|
|
for example if you have a
|
|
|
|
:ref:`custom manager<custom-managers>`::
|
|
|
|
|
|
|
|
get_object_or_404(Book.dahl_objects, title='Matilda')
|
|
|
|
|
|
|
|
You can also use
|
|
|
|
:class:`related managers<django.db.models.fields.related.RelatedManager>`::
|
|
|
|
|
|
|
|
author = Author.objects.get(name='Roald Dahl')
|
|
|
|
get_object_or_404(author.book_set, title='Matilda')
|
|
|
|
|
2010-12-05 16:47:39 +00:00
|
|
|
Note: As with ``get()``, a
|
|
|
|
:class:`~django.core.exceptions.MultipleObjectsReturned` exception
|
|
|
|
will be raised if more than one object is found.
|
2007-09-14 22:33:06 +00:00
|
|
|
|
2007-09-14 21:09:38 +00:00
|
|
|
``get_list_or_404``
|
|
|
|
===================
|
|
|
|
|
2009-03-21 13:09:13 +00:00
|
|
|
.. function:: get_list_or_404(klass, *args, **kwargs)
|
|
|
|
|
2011-03-08 19:51:19 +00:00
|
|
|
Returns the result of :meth:`~django.db.models.query.QuerySet.filter()` on a
|
2013-06-24 11:00:53 +00:00
|
|
|
given model manager cast to a list, raising :class:`~django.http.Http404` if
|
|
|
|
the resulting list is empty.
|
2007-09-14 22:33:06 +00:00
|
|
|
|
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``klass``
|
2010-12-05 16:47:39 +00: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-14 22:33:06 +00: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 06:06:56 +00:00
|
|
|
my_objects = list(MyModel.objects.filter(published=True))
|
2007-09-14 22:33:06 +00:00
|
|
|
if not my_objects:
|
2015-01-13 08:02:17 +00:00
|
|
|
raise Http404("No MyModel matches the given query.")
|