2012-06-11 08:34:00 +00:00
|
|
|
==========
|
|
|
|
Base views
|
|
|
|
==========
|
|
|
|
|
|
|
|
The following three classes provide much of the functionality needed to create
|
|
|
|
Django views. You may think of them as *parent* views, which can be used by
|
|
|
|
themselves or inherited from. They may not provide all the capabilities
|
|
|
|
required for projects, in which case there are Mixins and Generic class-based
|
|
|
|
views.
|
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
Many of Django's built-in class-based views inherit from other class-based
|
2013-05-16 13:56:30 +00:00
|
|
|
views or various mixins. Because this inheritance chain is very important, the
|
2012-09-08 17:46:08 +00:00
|
|
|
ancestor classes are documented under the section title of **Ancestors (MRO)**.
|
|
|
|
MRO is an acronym for Method Resolution Order.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``View``
|
|
|
|
========
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. class:: django.views.generic.base.View
|
|
|
|
|
2021-11-22 10:47:38 +00:00
|
|
|
The base view class. All other class-based views inherit from this base
|
|
|
|
class. It isn't strictly a generic view and thus can also be imported from
|
|
|
|
``django.views``.
|
2016-01-09 11:40:08 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
**Method Flowchart**
|
|
|
|
|
2018-09-19 08:53:05 +00:00
|
|
|
#. :meth:`setup()`
|
2018-11-15 18:54:28 +00:00
|
|
|
#. :meth:`dispatch()`
|
|
|
|
#. :meth:`http_method_not_allowed()`
|
|
|
|
#. :meth:`options()`
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
**Example views.py**::
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
2016-01-09 11:40:08 +00:00
|
|
|
from django.views import View
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyView(View):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return HttpResponse("Hello, World!")
|
|
|
|
|
|
|
|
**Example urls.py**::
|
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
from django.urls import path
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
from myapp.views import MyView
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2016-10-20 17:29:04 +00:00
|
|
|
path("mine/", MyView.as_view(), name="my-view"),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
**Attributes**
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
.. attribute:: http_method_names
|
2012-09-08 17:46:08 +00:00
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
The list of HTTP method names that this view will accept.
|
|
|
|
|
|
|
|
Default::
|
|
|
|
|
2013-05-21 16:01:29 +00:00
|
|
|
["get", "post", "put", "patch", "delete", "head", "options", "trace"]
|
2012-09-08 17:46:08 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
**Methods**
|
|
|
|
|
2012-09-08 17:19:58 +00:00
|
|
|
.. classmethod:: as_view(**initkwargs)
|
|
|
|
|
|
|
|
Returns a callable view that takes a request and returns a response::
|
|
|
|
|
2012-09-08 21:34:22 +00:00
|
|
|
response = MyView.as_view()(request)
|
2012-09-08 17:19:58 +00:00
|
|
|
|
2014-12-27 07:16:53 +00:00
|
|
|
The returned view has ``view_class`` and ``view_initkwargs``
|
|
|
|
attributes.
|
|
|
|
|
2017-06-25 19:17:12 +00:00
|
|
|
When the view is called during the request/response cycle, the
|
2018-09-19 08:53:05 +00:00
|
|
|
:meth:`setup` method assigns the :class:`~django.http.HttpRequest` to
|
|
|
|
the view's ``request`` attribute, and any positional and/or keyword
|
|
|
|
arguments :ref:`captured from the URL pattern
|
|
|
|
<how-django-processes-a-request>` to the ``args`` and ``kwargs``
|
|
|
|
attributes, respectively. Then :meth:`dispatch` is called.
|
|
|
|
|
2022-04-07 05:05:59 +00:00
|
|
|
If a ``View`` subclass defines asynchronous (``async def``) method
|
|
|
|
handlers, ``as_view()`` will mark the returned callable as a coroutine
|
|
|
|
function. An ``ImproperlyConfigured`` exception will be raised if both
|
|
|
|
asynchronous (``async def``) and synchronous (``def``) handlers are
|
|
|
|
defined on a single view-class.
|
|
|
|
|
2018-09-19 08:53:05 +00:00
|
|
|
.. method:: setup(request, *args, **kwargs)
|
|
|
|
|
2019-10-10 05:22:43 +00:00
|
|
|
Performs key view initialization prior to :meth:`dispatch`.
|
2018-09-19 08:53:05 +00:00
|
|
|
|
2019-10-10 05:22:43 +00:00
|
|
|
If overriding this method, you must call ``super()``.
|
2017-06-25 19:17:12 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. method:: dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
The ``view`` part of the view -- the method that accepts a ``request``
|
2021-10-18 16:06:00 +00:00
|
|
|
argument plus arguments, and returns an HTTP response.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
The default implementation will inspect the HTTP method and attempt to
|
|
|
|
delegate to a method that matches the HTTP method; a ``GET`` will be
|
2013-01-01 13:12:42 +00:00
|
|
|
delegated to ``get()``, a ``POST`` to ``post()``, and so on.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
By default, a ``HEAD`` request will be delegated to ``get()``.
|
2012-09-08 22:45:02 +00:00
|
|
|
If you need to handle ``HEAD`` requests in a different way than ``GET``,
|
2013-01-01 13:12:42 +00:00
|
|
|
you can override the ``head()`` method. See
|
2012-09-08 22:45:02 +00:00
|
|
|
:ref:`supporting-other-http-methods` for an example.
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. method:: http_method_not_allowed(request, *args, **kwargs)
|
|
|
|
|
2021-10-18 16:06:00 +00:00
|
|
|
If the view was called with an HTTP method it doesn't support, this
|
2012-06-11 08:34:00 +00:00
|
|
|
method is called instead.
|
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
The default implementation returns ``HttpResponseNotAllowed`` with a
|
|
|
|
list of allowed methods in plain text.
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
.. method:: options(request, *args, **kwargs)
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2016-06-01 12:01:05 +00:00
|
|
|
Handles responding to requests for the OPTIONS HTTP verb. Returns a
|
|
|
|
response with the ``Allow`` header containing a list of the view's
|
|
|
|
allowed HTTP method names.
|
|
|
|
|
2022-04-07 05:05:59 +00:00
|
|
|
If the other HTTP methods handlers on the class are asynchronous
|
|
|
|
(``async def``) then the response will be wrapped in a coroutine
|
|
|
|
function for use with ``await``.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``TemplateView``
|
|
|
|
================
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. class:: django.views.generic.base.TemplateView
|
|
|
|
|
2020-08-24 07:00:12 +00:00
|
|
|
Renders a given template, with the context containing parameters captured
|
|
|
|
in the URL.
|
2013-02-01 08:55:19 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
This view inherits methods and attributes from the following views:
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
2014-01-16 12:58:08 +00:00
|
|
|
* :class:`django.views.generic.base.ContextMixin`
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Method Flowchart**
|
|
|
|
|
2018-09-19 08:53:05 +00:00
|
|
|
#. :meth:`~django.views.generic.base.View.setup()`
|
2018-11-15 18:54:28 +00:00
|
|
|
#. :meth:`~django.views.generic.base.View.dispatch()`
|
|
|
|
#. :meth:`~django.views.generic.base.View.http_method_not_allowed()`
|
|
|
|
#. :meth:`~django.views.generic.base.ContextMixin.get_context_data()`
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
**Example views.py**::
|
|
|
|
|
|
|
|
from django.views.generic.base import TemplateView
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
from articles.models import Article
|
|
|
|
|
|
|
|
|
|
|
|
class HomePageView(TemplateView):
|
|
|
|
template_name = "home.html"
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-22 06:57:14 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2012-06-11 08:34:00 +00:00
|
|
|
context["latest_articles"] = Article.objects.all()[:5]
|
|
|
|
return context
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
**Example urls.py**::
|
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
from django.urls import path
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
from myapp.views import HomePageView
|
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2016-10-20 17:29:04 +00:00
|
|
|
path("", HomePageView.as_view(), name="home"),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
**Context**
|
|
|
|
|
2020-08-24 07:00:12 +00:00
|
|
|
* Populated (through :class:`~django.views.generic.base.ContextMixin`) with
|
|
|
|
the keyword arguments captured from the URL pattern that served the view.
|
2017-07-06 14:34:54 +00:00
|
|
|
* You can also add context using the
|
|
|
|
:attr:`~django.views.generic.base.ContextMixin.extra_context` keyword
|
|
|
|
argument for :meth:`~django.views.generic.base.View.as_view`.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``RedirectView``
|
|
|
|
================
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. class:: django.views.generic.base.RedirectView
|
|
|
|
|
|
|
|
Redirects to a given URL.
|
|
|
|
|
|
|
|
The given URL may contain dictionary-style string formatting, which will be
|
|
|
|
interpolated against the parameters captured in the URL. Because keyword
|
|
|
|
interpolation is *always* done (even if no arguments are passed in), any
|
|
|
|
``"%"`` characters in the URL must be written as ``"%%"`` so that Python
|
|
|
|
will convert them to a single percent sign on output.
|
|
|
|
|
|
|
|
If the given URL is ``None``, Django will return an ``HttpResponseGone``
|
|
|
|
(410).
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
This view inherits methods and attributes from the following view:
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Method Flowchart**
|
|
|
|
|
2018-09-19 08:53:05 +00:00
|
|
|
#. :meth:`~django.views.generic.base.View.setup()`
|
2018-11-15 18:54:28 +00:00
|
|
|
#. :meth:`~django.views.generic.base.View.dispatch()`
|
|
|
|
#. :meth:`~django.views.generic.base.View.http_method_not_allowed()`
|
|
|
|
#. :meth:`get_redirect_url()`
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
**Example views.py**::
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.views.generic.base import RedirectView
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
from articles.models import Article
|
|
|
|
|
|
|
|
|
|
|
|
class ArticleCounterRedirectView(RedirectView):
|
|
|
|
permanent = False
|
|
|
|
query_string = True
|
2013-06-14 10:59:26 +00:00
|
|
|
pattern_name = "article-detail"
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2013-06-14 10:59:26 +00:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2014-07-16 16:08:24 +00:00
|
|
|
article = get_object_or_404(Article, pk=kwargs["pk"])
|
2012-06-11 08:34:00 +00:00
|
|
|
article.update_counter()
|
2017-01-22 06:57:14 +00:00
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
**Example urls.py**::
|
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
from django.urls import path
|
2012-06-11 08:34:00 +00:00
|
|
|
from django.views.generic.base import RedirectView
|
2012-08-11 06:07:15 +00:00
|
|
|
|
2021-03-01 21:54:22 +00:00
|
|
|
from article.views import ArticleCounterRedirectView, ArticleDetailView
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2016-10-20 17:29:04 +00:00
|
|
|
path(
|
|
|
|
"counter/<int:pk>/",
|
|
|
|
ArticleCounterRedirectView.as_view(),
|
|
|
|
name="article-counter",
|
|
|
|
),
|
2021-03-01 21:54:22 +00:00
|
|
|
path("details/<int:pk>/", ArticleDetailView.as_view(), name="article-detail"),
|
2021-04-27 11:09:00 +00:00
|
|
|
path(
|
|
|
|
"go-to-django/",
|
|
|
|
RedirectView.as_view(url="https://www.djangoproject.com/"),
|
|
|
|
name="go-to-django",
|
|
|
|
),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2012-06-11 08:34:00 +00:00
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
**Attributes**
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
.. attribute:: url
|
|
|
|
|
|
|
|
The URL to redirect to, as a string. Or ``None`` to raise a 410 (Gone)
|
|
|
|
HTTP error.
|
|
|
|
|
2013-06-14 10:59:26 +00:00
|
|
|
.. attribute:: pattern_name
|
|
|
|
|
|
|
|
The name of the URL pattern to redirect to. Reversing will be done
|
|
|
|
using the same args and kwargs as are passed in for this view.
|
|
|
|
|
2012-06-11 08:34:00 +00:00
|
|
|
.. attribute:: permanent
|
|
|
|
|
|
|
|
Whether the redirect should be permanent. The only difference here is
|
|
|
|
the HTTP status code returned. If ``True``, then the redirect will use
|
|
|
|
status code 301. If ``False``, then the redirect will use status code
|
2015-01-18 21:43:57 +00:00
|
|
|
302. By default, ``permanent`` is ``False``.
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
.. attribute:: query_string
|
|
|
|
|
|
|
|
Whether to pass along the GET query string to the new location. If
|
|
|
|
``True``, then the query string is appended to the URL. If ``False``,
|
|
|
|
then the query string is discarded. By default, ``query_string`` is
|
|
|
|
``False``.
|
|
|
|
|
2012-09-08 17:46:08 +00:00
|
|
|
**Methods**
|
|
|
|
|
2013-11-07 11:30:04 +00:00
|
|
|
.. method:: get_redirect_url(*args, **kwargs)
|
2012-06-11 08:34:00 +00:00
|
|
|
|
|
|
|
Constructs the target URL for redirection.
|
|
|
|
|
2020-02-13 15:01:43 +00:00
|
|
|
The ``args`` and ``kwargs`` arguments are positional and/or keyword
|
|
|
|
arguments :ref:`captured from the URL pattern
|
|
|
|
<how-django-processes-a-request>`, respectively.
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
The default implementation uses :attr:`url` as a starting
|
2013-11-07 11:30:04 +00:00
|
|
|
string and performs expansion of ``%`` named parameters in that string
|
|
|
|
using the named groups captured in the URL.
|
|
|
|
|
|
|
|
If :attr:`url` is not set, ``get_redirect_url()`` tries to reverse the
|
|
|
|
:attr:`pattern_name` using what was captured in the URL (both named and
|
|
|
|
unnamed groups are used).
|
|
|
|
|
|
|
|
If requested by :attr:`query_string`, it will also append the query
|
|
|
|
string to the generated URL.
|
2013-01-01 13:12:42 +00:00
|
|
|
Subclasses may implement any behavior they wish, as long as the method
|
|
|
|
returns a redirect-ready URL string.
|