Django-class-based view source code analysis 2

Source: Internet
Author: User
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.

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

  2. Dispatch scheduling function. Call functions of the same name in the view based on the HTTP Method

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.