Base views

    Many of Django’s built-in class-based views inherit from other class-based views or various mixins. Because this inheritance chain is very important, the ancestor classes are documented under the section title of Ancestors (MRO). MRO is an acronym for Method Resolution Order.

    class django.views.generic.base.``View

    The master class-based base view. 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.

    Method Flowchart

    1. setup()
    2. http_method_not_allowed()

    Example views.py:

    Example urls.py:

    1. from django.urls import path
    2. from myapp.views import MyView
    3. urlpatterns = [
    4. path('mine/', MyView.as_view(), name='my-view'),
    5. ]

    Attributes

    • http_method_names

      The list of HTTP method names that this view will accept.

      Default:

    Methods

    • classmethod as_view(\*initkwargs*)

      Returns a callable view that takes a request and returns a response:

      1. response = MyView.as_view()(request)

      The returned view has view_class and view_initkwargs attributes.

      When the view is called during the request/response cycle, the setup() method assigns the to the view’s request attribute, and any positional and/or keyword arguments captured from the URL pattern to the args and kwargs attributes, respectively. Then is called.

    • setup(request, \args, **kwargs*)

      Performs key view initialization prior to .

      If overriding this method, you must call super().

    • http_method_not_allowed(request, \args, **kwargs*)

      If the view was called with a HTTP method it doesn’t support, this method is called instead.

      The default implementation returns HttpResponseNotAllowed with a list of allowed methods in plain text.

    • options(request, \args, **kwargs*)

      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.

    class django.views.generic.base.``TemplateView

    Renders a given template.

    Ancestors (MRO)

    This view inherits methods and attributes from the following views:

    Method Flowchart

    1. setup()
    2. http_method_not_allowed()

    Example views.py:

    Example urls.py:

    1. from django.urls import path
    2. from myapp.views import HomePageView
    3. urlpatterns = [
    4. path('', HomePageView.as_view(), name='home'),
    5. ]

    Context

    Deprecated since version 3.1: Starting in Django 4.0, the keyword arguments captured from the URL pattern won’t be passed to the context. Reference them with view.kwargs instead.

    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.

    Ancestors (MRO)

    This view inherits methods and attributes from the following view:

    Method Flowchart

    1. setup()
    2. http_method_not_allowed()

    Example views.py:

    Example urls.py:

    1. from django.urls import path
    2. from django.views.generic.base import RedirectView
    3. from article.views import ArticleCounterRedirectView, ArticleDetail
    4. urlpatterns = [
    5. path('counter/<int:pk>/', ArticleCounterRedirectView.as_view(), name='article-counter'),
    6. path('details/<int:pk>/', ArticleDetail.as_view(), name='article-detail'),
    7. path('go-to-django/', RedirectView.as_view(url='https://djangoproject.com'), name='go-to-django'),
    8. ]

    Attributes

    • url

      The URL to redirect to, as a string. Or None to raise a 410 (Gone) HTTP error.

    • 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.

    • 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 302. By default, permanent is False.

    • 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.

    Methods