基础视图
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.
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.
View
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
示例 views.py:
from django.http import HttpResponse
from django.views import View
class MyView(View):
def get(self, request, *args, **kwargs):
return HttpResponse('Hello, World!')
示例 urls.py:
from django.urls import path
from myapp.views import MyView
urlpatterns = [
path('mine/', MyView.as_view(), name='my-view'),
]
属性
-
The list of HTTP method names that this view will accept.
默认:
['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
方法
classmethod
as_view
(\*initkwargs*)Returns a callable view that takes a request and returns a response:
response = MyView.as_view()(request)
The returned view has
view_class
andview_initkwargs
attributes.When the view is called during the request/response cycle, the
setup()
method assigns theHttpRequest
to the view’srequest
attribute, and any positional and/or keyword arguments captured from the URL pattern to theargs
andkwargs
attributes, respectively. Thendispatch()
is called.setup
(request, \args, **kwargs*)Performs key view initialization prior to
dispatch()
.If overriding this method, you must call
super()
.dispatch
(request, \args, **kwargs*)The
view
part of the view — the method that accepts arequest
argument plus arguments, and returns a HTTP response.The default implementation will inspect the HTTP method and attempt to delegate to a method that matches the HTTP method; a
GET
will be delegated toget()
, aPOST
topost()
, and so on.By default, a
HEAD
request will be delegated toget()
. If you need to handleHEAD
requests in a different way thanGET
, you can override thehead()
method. See 支持其他 HTTP 方法 for an example.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.
TemplateView
class django.views.generic.base.``TemplateView
Renders a given template.
Ancestors (MRO)
This view inherits methods and attributes from the following views:
django.views.generic.base.TemplateResponseMixin
django.views.generic.base.ContextMixin
django.views.generic.base.View
Method Flowchart
示例 views.py:
from django.views.generic.base import TemplateView
from articles.models import Article
class HomePageView(TemplateView):
template_name = "home.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['latest_articles'] = Article.objects.all()[:5]
return context
示例 urls.py:
from django.urls import path
from myapp.views import HomePageView
urlpatterns = [
path('', HomePageView.as_view(), name='home'),
]
Context
- Populated (through
ContextMixin
). - You can also add context using the
extra_context
keyword argument foras_view()
.
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.
RedirectView
class django.views.generic.base.``RedirectView
重定向到指定的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)
这个视图从以下的视图中继承了方法和属性:
Method Flowchart
示例 views.py:
from django.shortcuts import get_object_or_404
from django.views.generic.base import RedirectView
from articles.models import Article
class ArticleCounterRedirectView(RedirectView):
permanent = False
query_string = True
pattern_name = 'article-detail'
def get_redirect_url(self, *args, **kwargs):
article = get_object_or_404(Article, pk=kwargs['pk'])
article.update_counter()
return super().get_redirect_url(*args, **kwargs)
示例 urls.py:
from django.urls import path
from django.views.generic.base import RedirectView
from article.views import ArticleCounterRedirectView, ArticleDetail
urlpatterns = [
path('counter/<int:pk>/', ArticleCounterRedirectView.as_view(), name='article-counter'),
path('details/<int:pk>/', ArticleDetail.as_view(), name='article-detail'),
path('go-to-django/', RedirectView.as_view(url='https://djangoproject.com'), name='go-to-django'),
]
属性
-
The URL to redirect to, as a string. Or
None
to raise a 410 (Gone) HTTP error. -
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.
-
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. IfFalse
, then the redirect will use status code 302. By default,permanent
isFalse
. -
Whether to pass along the GET query string to the new location. If
True
, then the query string is appended to the URL. IfFalse
, then the query string is discarded. By default,query_string
isFalse
.
方法
get_redirect_url
(\args, **kwargs*)构建用于重定向的目标URL。
The
args
andkwargs
arguments are positional and/or keyword arguments captured from the URL pattern, respectively.The default implementation uses
url
as a starting string and performs expansion of%
named parameters in that string using the named groups captured in the URL.If
url
is not set,get_redirect_url()
tries to reverse thepattern_name
using what was captured in the URL (both named and unnamed groups are used).If requested by
query_string
, it will also append the query string to the generated URL. Subclasses may implement any behavior they wish, as long as the method returns a redirect-ready URL string.