Django Function Based View (FBV) vs. class Based view (CBV)

Source: Internet
Author: User

First, the FBV processing process

First look at the FBV logic process:

1. Simple process (borrowing official examples):

1 URLs:2  fromDjango.conf.urlsImportURL3 4  from.Import views5 6Urlpatterns = [7URL (r'^$', Views.index, Name='Index'),8 ]9 Ten Views : One  fromDjango.httpImportHttpResponse A  -  from. ModelsImportQuestion -  the defIndex (Request): -Latest_question_list = Question.objects.order_by ('-pub_date') [: 5] -Output =', '. Join ([Q.question_text forQinchLatest_question_list]) -     returnHttpResponse (Output)

Step1: Based on the access request, find a matching URL map in the URLs, get Views.index

SETP2: According to Views.index, call the Views under the Index function (incoming parameter request is user requested information)

STEP3: Data processing According to customer request information, get the data output and context required by the user, return the client by HttpResponse

2. Expand each step in the 1

In Step1, parameters can be introduced and passed to the function processing

1 ......2 URLs:3 4URL (r'^(? p<question_id>[0-9]+)/$', Views.detail, Name='Detail'),5 ......6 Views :7 8 defdetail (Request, question_id):9     returnHttpResponse ("You ' re looking at question%s."% question_id)

Further expansion, reverse analysis such as reverse.

Step2 extension, such as according to the user to the database for the deletion and check operation, get querysets, or get other context contextual information

Step3 extension, you can pass the results to the template after rendering and return to the client:

If the content content context is rendered with template templates, it is passed to the request corresponding to the response. Render (Request, template_name, context, Content_Type, status, using)

1  fromDjango.shortcutsImportRender2 3 defMy_view (Request):4     #View code here ...5     returnRender (Request,'myapp/index.html', {"Foo":"Bar"},6Content_type="Application/xhtml+xml")7 8 #equivalent to the following9  fromDjango.httpImportHttpResponseTen  fromDjango.templateImportRequestContext, Loader One  A defMy_view (Request): -     #View code here ... -t = loader.get_template ('myapp/index.html') thec = RequestContext (Request, {'Foo':'Bar'}) -     returnHttpResponse (T.render (c), -Content_type="Application/xhtml+xml")

The FBV process is basically like this

Analysis on the process of two CBV

In fact, CBV process can be regarded as the abstraction and object of FBV process. He needs the most basic three classes of view,contextmixin,templateresponsemixin

Three steps corresponding to FBV:

Step1. The view class provides the class method As_view (), which is used to call Dipatch () and distribute to get,post according to the request type ... and other corresponding methods of processing.

Step2. The Contextmixin class, Get_context_data (self, **kwargs) Gets the context data, and if the operation on the database can inherit the class, then the result of the redaction is placed in the context data (that is, overriding Get_context_data)

Step3. Templateresponsemixin class, renders content onto the specified template and implements the corresponding function through the Render_to_response () method

The rest of the template view is basically the inheritance rewrite on these three classes to get.

1. Common Template View

Templateview (Templateresponsemixin, Contextmixin, View)

2. Single Object template view DetailView (Singleobjecttemplateresponsemixin,basedetailview)

One of the key classes is singleobjectminxin, which is to get a single piece of data from the database that the user requested.

3. Multiple object Templates View ListView (Multipleobjecttemplateresponsemixin, Baselistview)

One of the key classes is multipleobjectmixin, which gets the list of data requested by the user from the database.

4. Data new View CreateView (Singleobjecttemplateresponsemixin, Basecreateview)

Because the new data is usually submitted by the client through form, it is designed to the content of form form, such as the validity of the form data, the URL of the successful form submission, etc. All have formminxin base class generation.

5. Data Update template Updateview (Singleobjecttemplateresponsemixin, Baseupdateview)

For updating data, similar to CreateView

6. Data delete View Deleteview (Singleobjecttemplateresponsemixin, Basedeleteview)

The key class is deletionmixin, which deletes the corresponding data.

The archive view will be analyzed in subsequent articles, as well as in the view of the editor's presentation.

The properties and methods in the class can be found in the official documentation description. In this way, you can write the classes you need.

Django Function Based View (FBV) vs. class Based view (CBV)

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.