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.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``render()``
|
|
|
|
============
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2015-09-04 10:01:30 +08:00
|
|
|
.. function:: render(request, template_name, context=None, content_type=None, status=None, using=None)
|
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.
|
|
|
|
|
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``
|
2015-12-22 23:21:24 +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 loading documentation <template-loading>` for more
|
|
|
|
information on how templates are found.
|
2010-12-22 01:18:41 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
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``.
|
|
|
|
|
2015-01-27 04:57:10 +08:00
|
|
|
``using``
|
|
|
|
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
|
|
|
loading the template.
|
|
|
|
|
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...
|
2016-06-03 03:56:13 +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
|
2015-12-24 05:14:27 +08:00
|
|
|
from django.template import 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')
|
2015-12-24 05:14:27 +08:00
|
|
|
c = {'foo': 'bar'}
|
2016-06-03 03:56:13 +08:00
|
|
|
return HttpResponse(t.render(c, request), content_type='application/xhtml+xml')
|
2010-12-22 01:18:41 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``render_to_response()``
|
|
|
|
========================
|
2009-03-21 21:09:13 +08:00
|
|
|
|
2015-09-04 10:01:30 +08:00
|
|
|
.. function:: render_to_response(template_name, context=None, content_type=None, status=None, using=None)
|
2007-09-15 05:09:38 +08:00
|
|
|
|
2017-02-11 18:49:04 +08:00
|
|
|
.. deprecated:: 2.0
|
|
|
|
|
2015-12-22 23:21:24 +08:00
|
|
|
This function preceded the introduction of :func:`render` and works
|
2015-12-24 05:33:59 +08:00
|
|
|
similarly except that it doesn't make the ``request`` available in the
|
2017-02-11 18:49:04 +08:00
|
|
|
response.
|
2007-09-15 06:33:06 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``redirect()``
|
|
|
|
==============
|
2009-03-21 21:09:13 +08:00
|
|
|
|
2015-07-27 20:35:21 +08:00
|
|
|
.. function:: redirect(to, permanent=False, *args, **kwargs)
|
2009-03-21 21:09:13 +08:00
|
|
|
|
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
|
|
|
|
2015-12-30 23:51:16 +08:00
|
|
|
* A view name, possibly with arguments: :func:`~django.urls.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.
|
|
|
|
|
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):
|
|
|
|
...
|
2018-07-07 00:44:04 +08:00
|
|
|
obj = MyModel.objects.get(...)
|
|
|
|
return redirect(obj)
|
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
|
2015-12-30 23:51:16 +08:00
|
|
|
:func:`~django.urls.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):
|
|
|
|
...
|
2015-11-30 00:29:46 +08:00
|
|
|
return redirect('https://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):
|
|
|
|
...
|
2018-07-07 00:44:04 +08:00
|
|
|
obj = MyModel.objects.get(...)
|
|
|
|
return redirect(obj, permanent=True)
|
2009-03-21 21:09:13 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``get_object_or_404()``
|
|
|
|
=======================
|
2007-09-15 05:09:38 +08:00
|
|
|
|
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
|
2015-05-08 09:30:04 +08:00
|
|
|
:class:`~django.db.models.Model.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):
|
2018-07-07 00:44:04 +08:00
|
|
|
obj = get_object_or_404(MyModel, pk=1)
|
2007-09-15 06:33:06 +08:00
|
|
|
|
|
|
|
This example is equivalent to::
|
|
|
|
|
|
|
|
from django.http import Http404
|
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
try:
|
2018-07-07 00:44:04 +08:00
|
|
|
obj = MyModel.objects.get(pk=1)
|
2007-09-15 06:33:06 +08:00
|
|
|
except MyModel.DoesNotExist:
|
2015-01-13 16:02:17 +08:00
|
|
|
raise Http404("No MyModel matches the given query.")
|
2007-09-15 06:33:06 +08:00
|
|
|
|
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
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``get_list_or_404()``
|
|
|
|
=====================
|
2007-09-15 05:09:38 +08:00
|
|
|
|
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:
|
2015-01-13 16:02:17 +08:00
|
|
|
raise Http404("No MyModel matches the given query.")
|