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
.
The ability to import from django.views
was added.
Method Flowchart
dispatch()
http_method_not_allowed()
options()
Example 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!')
Example urls.py:
from django.conf.urls import url from myapp.views import MyView urlpatterns = [ url(r'^mine/$', MyView.as_view(), name='my-view'), ]
Attributes
-
http_method_names
-
The list of HTTP method names that this view will accept.
Default:
['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
Methods
-
classmethod as_view(**initkwargs)
-
Returns a callable view that takes a request and returns a response:
response = MyView.as_view()(request)
New in Django 1.9.The returned view has
view_class
andview_initkwargs
attributes.
-
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 Supporting other HTTP methods 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.
Please login to continue.