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``
|
|
|
|
==========
|
|
|
|
|
2015-09-04 04:01:30 +02:00
|
|
|
.. function:: render(request, template_name, context=None, content_type=None, status=None, using=None)
|
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.
|
|
|
|
|
2013-12-11 00:24:31 +01: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 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``
|
2015-12-22 16:21:24 +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 loading documentation <template-loading>` for more
|
|
|
|
information on how templates are found.
|
2010-12-21 18:18:41 +01:00
|
|
|
|
|
|
|
Optional arguments
|
|
|
|
------------------
|
|
|
|
|
2014-12-14 10:17:18 +01:00
|
|
|
``context``
|
2010-12-21 18:18:41 +01: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.
|
|
|
|
|
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.
|
|
|
|
|
2010-12-22 05:54:10 +01:00
|
|
|
``status``
|
|
|
|
The status code for the response. Defaults to ``200``.
|
|
|
|
|
2015-01-26 21:57:10 +01:00
|
|
|
``using``
|
|
|
|
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
|
|
|
loading the template.
|
|
|
|
|
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
|
2015-12-23 22:14:27 +01:00
|
|
|
from django.template import loader
|
2010-12-21 18:18:41 +01:00
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
# View code here...
|
2013-11-15 13:09:46 +01:00
|
|
|
t = loader.get_template('myapp/index.html')
|
2015-12-23 22:14:27 +01:00
|
|
|
c = {'foo': 'bar'}
|
|
|
|
return HttpResponse(t.render(c, request),
|
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``
|
|
|
|
======================
|
|
|
|
|
2015-09-04 04:01:30 +02:00
|
|
|
.. function:: render_to_response(template_name, context=None, content_type=None, status=None, using=None)
|
2007-09-14 23:09:38 +02:00
|
|
|
|
2015-12-22 16:21:24 +01:00
|
|
|
This function preceded the introduction of :func:`render` and works
|
|
|
|
similarly except that it doesn't making the ``request`` available in the
|
|
|
|
response. It's not recommended and is likely to be deprecated in the future.
|
2007-09-15 00:33:06 +02:00
|
|
|
|
2009-03-21 14:09:13 +01:00
|
|
|
``redirect``
|
|
|
|
============
|
|
|
|
|
2015-07-27 14:35:21 +02:00
|
|
|
.. function:: redirect(to, permanent=False, *args, **kwargs)
|
2009-03-21 14:09:13 +01:00
|
|
|
|
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
|
|
|
|
2013-10-14 13:59:56 +02:00
|
|
|
* A model: the model's :meth:`~django.db.models.Model.get_absolute_url()`
|
2013-08-15 13:14:10 +02:00
|
|
|
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
|
|
|
|
2014-02-14 04:48:44 +01:00
|
|
|
* An absolute or relative 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
|
2014-02-14 04:48:44 +01:00
|
|
|
permanent redirect.
|
|
|
|
|
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):
|
|
|
|
...
|
2015-11-29 17:29:46 +01:00
|
|
|
return redirect('https://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
|
2015-05-08 03:30:04 +02:00
|
|
|
:class:`~django.db.models.Model.DoesNotExist` exception.
|
2007-09-14 23:09:38 +02:00
|
|
|
|
2007-09-15 00:33:06 +02:00
|
|
|
Required arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``klass``
|
2013-11-19 15:25:15 +01: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 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:
|
2015-01-13 09:02:17 +01:00
|
|
|
raise Http404("No MyModel matches the given query.")
|
2007-09-15 00:33:06 +02:00
|
|
|
|
2013-11-19 15:25:15 +01: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 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
|
2013-06-24 13:00:53 +02:00
|
|
|
given model manager cast to a list, 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:
|
2015-01-13 09:02:17 +01:00
|
|
|
raise Http404("No MyModel matches the given query.")
|