Explanation of the CBV method of Django

Source: Internet
Author: User
CBV uses the configuration path URL

CBV is called through the class method. We configure the following path in the URL:

 url(r‘^cbv.html/‘, views.Cbv.as_view()),

Here, CBV is a class. To use the CBV method, the path must be followed by an as_view (). This is a required fixed format.

Function Format in views

To configure classes in views, You need to import a module

From Django. views. generic import view # This is the imported module. The original Django version is from Django. views can be directly imported into the view, but now you need to add a generic to the class CBV (View): # Here you must inherit the View class, only the as_view () that inherits the URL () this method def get (self, request): Return httpresponse ('cbv-get') def post (self, request): Return httpresponse ('cbv-post ')
Browser get access

Create a login Login page to test the POST method's views Configuration
From Django. views. generic import viewclass CBV (View): def get (self, request): # Return httpresponse ('cbv-get') return render(request,'login.html ') sent to login.html def post (self, request ): return httpresponse ('cbv-post ')

Page configuration code of login

<! Doctype HTML> <HTML lang = "en"> 
Browser access to view the results after clicking submit

Click to submit

Here, by viewing the view source code, we can see that there are many submission methods.
http_method_names = [‘get‘, ‘post‘, ‘put‘, ‘patch‘, ‘delete‘, ‘head‘, ‘options‘, ‘trace‘]
These methods can be used when Ajax is used.

In addition, the inheritance class not only has a view, but also has a lot to do. You can view the source code.

My Django version is
C: \ Users \ Tony> python3-M Django -- version
1.9.13

CBV Matching Principle

This more URL-based matching method is done through the reflection method (getattr. After the request comes, use the dispatch method. This method exists in the View class.

    def dispatch(self, request, *args, **kwargs):        # Try to dispatch to the right method; if a method doesn‘t exist,        # defer to the error handler. Also defer to the error handler if the        # request method isn‘t on the approved list.        if request.method.lower() in self.http_method_names:            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)        else:            handler = self.http_method_not_allowed        return handler(request, *args, **kwargs)
Customize dispatch

If you need to Perform Batch operations on methods such as get and post, you can manually write a dispatch here, which is the same as that of the decorator. Because when requests come, they always go first by dispatch.

From Django. views. generic import viewclass CBV (View): def dispatch (self, request, * ARGs, ** kwargs): Print ('Operation before operation') OBJ = super (CBV, self ). dispatch (request, * ARGs, ** kwargs) print ('Operation code after the operation') return OBJ def get (self, request ): # Return httpresponse ('cbv-get') return render(request,'login.html ') def post (self, request): Return httpresponse ('cbv-post ')

When we access the service through a browser this time, we find that no matter whether the get or POST method is used, the print code is used,

 

Explanation of the CBV method of Django

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.