2007-09-15 05:09:38 +08:00
|
|
|
=========================
|
|
|
|
Django shortcut functions
|
|
|
|
=========================
|
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
.. module:: django.shortcuts
|
|
|
|
:synopsis:
|
2012-11-02 16:29:55 +08:00
|
|
|
Convenience shortcuts that span multiple levels of Django's MVC stack.
|
2009-03-21 21:09:13 +08:00
|
|
|
|
|
|
|
.. index:: shortcuts
|
|
|
|
|
2007-09-15 05:09:38 +08: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-22 01:18:41 +08:00
|
|
|
``render``
|
|
|
|
==========
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
.. function:: render(request, template_name[, context][, context_instance][, content_type][, status][, current_app][, dirs])
|
2010-12-22 01:18:41 +08: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 19:14:10 +08:00
|
|
|
:func:`render_to_response()` with a ``context_instance`` argument that
|
2011-03-29 18:24:42 +08:00
|
|
|
forces the use of a :class:`~django.template.RequestContext`.
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2013-12-11 07:24:31 +08: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-22 01:18:41 +08:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``request``
|
|
|
|
The request object used to generate this response.
|
|
|
|
|
2012-06-21 07:13:35 +08:00
|
|
|
``template_name``
|
2010-12-22 01:18:41 +08:00
|
|
|
The full name of a template to use or sequence of template names.
|
|
|
|
|
|
|
|
Optional arguments
|
|
|
|
------------------
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
``context``
|
2010-12-22 01:18:41 +08: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 17:17:18 +08: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-22 01:18:41 +08: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 17:17:18 +08:00
|
|
|
``request`` and ``context``).
|
|
|
|
|
|
|
|
.. deprecated:: 1.8
|
|
|
|
|
|
|
|
The ``context_instance`` argument is deprecated. Simply use ``context``.
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2010-12-22 12:54:10 +08:00
|
|
|
``content_type``
|
2010-12-22 01:18:41 +08:00
|
|
|
The MIME type to use for the resulting document. Defaults to the value of
|
|
|
|
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
|
|
|
|
2010-12-22 12:54:10 +08:00
|
|
|
``status``
|
|
|
|
The status code for the response. Defaults to ``200``.
|
|
|
|
|
2011-01-06 06:41:43 +08: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-15 00:48:51 +08:00
|
|
|
.. deprecated:: 1.8
|
|
|
|
|
|
|
|
The ``current_app`` argument is deprecated. Instead you should set
|
|
|
|
``request.current_app``.
|
|
|
|
|
2013-09-16 05:51:24 +08:00
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
The ``dirs`` parameter was added.
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
.. deprecated:: 1.8
|
2014-11-14 03:25:08 +08:00
|
|
|
|
|
|
|
The ``dirs`` parameter was deprecated.
|
|
|
|
|
2010-12-22 01:18:41 +08:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example renders the template ``myapp/index.html`` with the
|
2011-09-05 05:17:30 +08:00
|
|
|
MIME type :mimetype:`application/xhtml+xml`::
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2010-12-22 12:54:10 +08:00
|
|
|
from django.shortcuts import render
|
2010-12-22 01:18:41 +08:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
2010-12-22 12:54:10 +08:00
|
|
|
return render(request, 'myapp/index.html', {"foo": "bar"},
|
|
|
|
content_type="application/xhtml+xml")
|
2010-12-22 01:18:41 +08:00
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
2011-06-27 00:52:50 +08:00
|
|
|
from django.template import RequestContext, loader
|
2010-12-22 01:18:41 +08:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
2013-11-15 20:09:46 +08:00
|
|
|
t = loader.get_template('myapp/index.html')
|
2010-12-22 01:18:41 +08:00
|
|
|
c = RequestContext(request, {'foo': 'bar'})
|
|
|
|
return HttpResponse(t.render(c),
|
2010-12-22 12:54:10 +08:00
|
|
|
content_type="application/xhtml+xml")
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
``render_to_response``
|
|
|
|
======================
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
.. function:: render_to_response(template_name[, context][, context_instance][, content_type][, status][, dirs])
|
2007-09-15 05:09:38 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
Renders a given template with a given context dictionary and returns an
|
|
|
|
:class:`~django.http.HttpResponse` object with that rendered text.
|
2007-09-15 05:09:38 +08:00
|
|
|
|
2007-09-15 06:33:06 +08:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
2011-05-19 04:33:12 +08:00
|
|
|
``template_name``
|
2010-12-05 04:20:54 +08: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 06:33:06 +08:00
|
|
|
|
|
|
|
Optional arguments
|
|
|
|
------------------
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
``context``
|
2007-09-15 06:33:06 +08: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 06:25:40 +08:00
|
|
|
view will call it just before rendering the template.
|
2007-09-15 06:33:06 +08:00
|
|
|
|
2014-12-14 17:17:18 +08: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-02-01 06:49:14 +08:00
|
|
|
``context_instance``
|
|
|
|
The context instance to render the template with. By default, the template
|
2010-12-06 00:47:39 +08:00
|
|
|
will be rendered with a :class:`~django.template.Context` instance (filled
|
2014-12-14 17:17:18 +08:00
|
|
|
with values from ``context``). If you need to use :ref:`context
|
2010-12-06 00:47:39 +08:00
|
|
|
processors <subclassing-context-requestcontext>`, render the template with
|
|
|
|
a :class:`~django.template.RequestContext` instance instead. Your code
|
|
|
|
might look something like this::
|
2008-02-01 06:49:14 +08:00
|
|
|
|
|
|
|
return render_to_response('my_template.html',
|
2014-12-14 17:17:18 +08:00
|
|
|
my_context,
|
2008-02-01 06:49:14 +08:00
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
.. deprecated:: 1.8
|
|
|
|
|
|
|
|
The ``context_instance`` argument is deprecated. Simply use ``context``.
|
|
|
|
|
2013-01-31 20:39:29 +08:00
|
|
|
``content_type``
|
2008-09-02 11:40:42 +08:00
|
|
|
The MIME type to use for the resulting document. Defaults to the value of
|
|
|
|
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
2007-09-15 06:33:06 +08:00
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
``status``
|
|
|
|
The status code for the response. Defaults to ``200``.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.8
|
|
|
|
|
|
|
|
The ``status`` parameter was added.
|
|
|
|
|
2013-09-16 05:51:24 +08:00
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
The ``dirs`` parameter was added.
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
.. deprecated:: 1.8
|
2014-11-14 03:25:08 +08:00
|
|
|
|
|
|
|
The ``dirs`` parameter was deprecated.
|
|
|
|
|
2007-09-15 06:33:06 +08:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example renders the template ``myapp/index.html`` with the
|
2011-09-05 05:17:30 +08:00
|
|
|
MIME type :mimetype:`application/xhtml+xml`::
|
2007-09-15 05:09:38 +08:00
|
|
|
|
|
|
|
from django.shortcuts import render_to_response
|
2007-09-15 06:33:06 +08:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
|
|
|
return render_to_response('myapp/index.html', {"foo": "bar"},
|
2014-08-07 08:32:41 +08:00
|
|
|
content_type="application/xhtml+xml")
|
2007-09-15 05:09:38 +08:00
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template import Context, loader
|
2007-09-15 06:33:06 +08:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
2013-11-15 23:21:58 +08:00
|
|
|
t = loader.get_template('myapp/index.html')
|
2007-09-15 06:33:06 +08:00
|
|
|
c = Context({'foo': 'bar'})
|
2009-04-01 00:49:54 +08:00
|
|
|
return HttpResponse(t.render(c),
|
2013-01-31 20:39:29 +08:00
|
|
|
content_type="application/xhtml+xml")
|
2007-09-15 06:33:06 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
``redirect``
|
|
|
|
============
|
|
|
|
|
|
|
|
.. function:: redirect(to[, permanent=False], *args, **kwargs)
|
|
|
|
|
2010-12-06 00:47:39 +08:00
|
|
|
Returns an :class:`~django.http.HttpResponseRedirect` to the appropriate URL
|
|
|
|
for the arguments passed.
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
The arguments could be:
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2013-10-14 19:59:56 +08:00
|
|
|
* A model: the model's :meth:`~django.db.models.Model.get_absolute_url()`
|
2013-08-15 19:14:10 +08:00
|
|
|
function will be called.
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2013-03-22 17:50:45 +08: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 23:26:56 +08:00
|
|
|
|
2014-02-14 11:48:44 +08:00
|
|
|
* An absolute or relative URL, which will be used as-is for the redirect
|
|
|
|
location.
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2010-12-06 00:47:39 +08:00
|
|
|
By default issues a temporary redirect; pass ``permanent=True`` to issue a
|
2014-02-14 11:48:44 +08:00
|
|
|
permanent redirect.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
The ability to use relative URLs was added.
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
|
|
|
You can use the :func:`redirect` function in a number of ways.
|
|
|
|
|
2011-10-14 08:12:01 +08: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 23:26:56 +08:00
|
|
|
|
2012-04-22 23:58:53 +08:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
object = MyModel.objects.get(...)
|
|
|
|
return redirect(object)
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2011-10-14 08:12:01 +08: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 23:26:56 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('some-view-name', foo='bar')
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
3. By passing a hardcoded URL to redirect to::
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('/some/url/')
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
This also works with full URLs::
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
return redirect('http://example.com/')
|
2009-03-21 23:26:56 +08:00
|
|
|
|
2009-03-21 21:09:13 +08: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-15 05:09:38 +08:00
|
|
|
``get_object_or_404``
|
|
|
|
=====================
|
|
|
|
|
2009-09-13 06:41:12 +08:00
|
|
|
.. function:: get_object_or_404(klass, *args, **kwargs)
|
2009-03-21 21:09:13 +08:00
|
|
|
|
2011-03-09 03:51:19 +08:00
|
|
|
Calls :meth:`~django.db.models.query.QuerySet.get()` on a given model manager,
|
2010-12-06 00:47:39 +08:00
|
|
|
but it raises :class:`~django.http.Http404` instead of the model's
|
|
|
|
:class:`~django.core.exceptions.DoesNotExist` exception.
|
2007-09-15 05:09:38 +08:00
|
|
|
|
2007-09-15 06:33:06 +08:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``klass``
|
2013-11-19 22:25:15 +08: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-15 06:33:06 +08: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 15:19:56 +08:00
|
|
|
my_object = MyModel.objects.get(pk=1)
|
2007-09-15 06:33:06 +08:00
|
|
|
except MyModel.DoesNotExist:
|
|
|
|
raise Http404
|
|
|
|
|
2013-11-19 22:25:15 +08: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-06 00:47:39 +08: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 06:33:06 +08:00
|
|
|
|
2007-09-15 05:09:38 +08:00
|
|
|
``get_list_or_404``
|
|
|
|
===================
|
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
.. function:: get_list_or_404(klass, *args, **kwargs)
|
|
|
|
|
2011-03-09 03:51:19 +08:00
|
|
|
Returns the result of :meth:`~django.db.models.query.QuerySet.filter()` on a
|
2013-06-24 19:00:53 +08:00
|
|
|
given model manager cast to a list, raising :class:`~django.http.Http404` if
|
|
|
|
the resulting list is empty.
|
2007-09-15 06:33:06 +08:00
|
|
|
|
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``klass``
|
2010-12-06 00:47:39 +08: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 06:33:06 +08: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 14:06:56 +08:00
|
|
|
my_objects = list(MyModel.objects.filter(published=True))
|
2007-09-15 06:33:06 +08:00
|
|
|
if not my_objects:
|
2008-09-02 11:40:42 +08:00
|
|
|
raise Http404
|