Source code analysis Abstract class and common view (base. py)
This file contains the view's top-level abstract class (view), template-based tool class (templateresponsemixin), template view (templateview), and redirectview ).
View and view execution sequence
View is the base class of all class-based views. Only some basic methods and necessary checks are implemented. The most important method is the dispatch method. In the next method, the corresponding handler with the same name is called based on the method parameter in the HTTP request. Here, we leave a remark. The subsequent class needs to fill in the phrase according to its own situation and finally process a view.
Attribute
Http_method_names defines all HTTP metho ['get', 'post', 'put', 'delete', 'head', 'options', 'track'].
Method
InitInitialization Method
Called by urlconf. Includes the function of saving keyword parameters to instance attributes.
If a view contains an instance attribute and a value is set in urlpattern. The method for assigning the configured value to the view object is called by urlconf.InitFunction is passed in as a parameter. View.InitAssign values.
As_view class method (classonlymethod ). Returns a function object in the function-based view. The returned view function object ("when called") is used to instantiate the view and call the dispatch method of the view.
Dispatch scheduling function. Call functions of the same name in the view based on the HTTP Method
Http_method_not_allowed: returns an unsupported HTTP Method processing function.
Templateresponsemixin
Provides a tool class for rendering with templates. The template_name parameter is used to specify a template. A view with the template function is expected to inherit the accumulative amount of this tool. However, we seldom inherit this tool class directly from our own views, because Django already provides a set of useful views to reduce our work.
Templateview
Template-based view. Additional data is required. You can override the get_context_data method in the subclass that inherits this view. It is often used for text-based static content such as "about", "copyrights", and "terms. You only need to set template_name. You can even directly specify this parameter in urlconf.
For example:
?
| 1 |
url(r‘^about/$‘,TemplateView.as_view(template_name=‘about.html‘)) |
Redirectview
Redirection view. This view provides a full redirection function. Both http get, post, delete, Head, and options are redirected to the address specified by the URL parameter.
The permanent attribute specifies whether to return permanent (HTTP 301) redirection information. Otherwise, a temporary redirect (HTTP 302) is returned. The default value is true.
Django-class-based view source code analysis 2