Comparison between Django Function Based View (FBV) and Class Based View (CBV), djangofbv

Source: Internet
Author: User

Comparison between Django Function Based View (FBV) and Class Based View (CBV), djangofbv

I. FBV processing process

First, let's take a look at the logic process of FBV:

1. Simple process (borrow an official example ):

 1 urls: 2 from django.conf.urls import url 3  4 from . import views 5  6 urlpatterns = [ 7     url(r'^$', views.index, name='index'), 8 ] 9 10 views:11 from django.http import HttpResponse12 13 from .models import Question14 15 def index(request):16     latest_question_list = Question.objects.order_by('-pub_date')[:5]17     output = ', '.join([q.question_text for q in latest_question_list])18     return HttpResponse(output)

Step 1: Search for matched url ing in urls Based on the access request to obtain views. index.

Setp2: Call the index function under views based on views. index (the request parameter is the user request information)

Step 3: process the data based on the customer's request information to obtain the user's required data output and context, and return the client through HttpResponse

2. Expand each step in step 1

Parameters can be introduced in step 1 and passed to functions in views for processing.

1 ......2 urls:3 4 url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),5 ......6 views:7 8 def detail(request, question_id):9     return HttpResponse("You're looking at question %s." % question_id)

Further expansion, reverse parsing, such as reverse.

Extended in step 2, such as adding, deleting, modifying, and querying the database based on the user to obtain querysets or other context information

Step 3 extension: You can pass the result to the template rendering and return it to the client:

For example, you can use the template to render the Content context and pass it to the response corresponding to the request. Render (request, template_name, context, content_type, status, using)

1 from django. shortcuts import render 2 3 def my_view (request): 4 # View code here... 5 return render (request, 'myapp/index.html ', {"foo": "bar"}, 6 content_type = "application/xhtml + xml ") 7 8 # equivalent to the following 9 from django. http import HttpResponse10 from django. template import RequestContext, loader11 12 def my_view (request): 13 # View code here... 14 t = loader. get_template ('myapp/index.html ') 15 c = RequestContext (request, {'foo': 'bar'}) 16 return HttpResponse (t. render (c), 17 content_type = "application/xhtml + xml ")

The FBV process is basically like this.

Analysis of CBV Process

In fact, the CBV process can be seen as the abstraction and objectization of the fbv process. He needs three basic classes: View, ContextMixin, and TemplateResponseMixin.

Three steps corresponding to FBV:

Step1. The View class provides the class method as_view (), which is used to call dipatch () and distribute it to get, post... and other corresponding methods for processing according to the request type.

Step2. ContextMixin class, get_context_data (self, ** kwargs) to get context data. If you perform operations on the database, you can inherit this class, then, add, delete, modify, and query results to the context data (that is, rewrite get_context_data)

Step3. TemplateResponseMixin class, rendering the content to the specified template, and implementing corresponding functions through the render_to_response () method

Other template views are basically inherited and overwritten on these three classes.

1. General template View

TemplateView (TemplateResponseMixin, ContextMixin, View)

2. DetailView (SingleObjectTemplateResponseMixin, BaseDetailView)

 

One of the key classes is SingleObjectMinxin, which obtains a single piece of data requested by the user from the database.

3. Multiple object template views ListView (MultipleObjectTemplateResponseMixin, BaseListView)

One of the key classes is MultipleObjectMixin, which obtains the user request data list from the database.

4. added the data view CreateView (SingleObjectTemplateResponseMixin, BaseCreateView)

 

Because newly added data is usually submitted by the client through form, the content related to the form is designed, such as the validity of the form data and the url of the form submitted successfully. Both are generated by the FormMinxin base class.

5. Data Update template UpdateView (SingleObjectTemplateResponseMixin, BaseUpdateView)

Used to update data, similar to CreateView

6. DeleteView (SingleObjectTemplateResponseMixin, BaseDeleteView)

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

 

The Archive view will be analyzed in subsequent articles, basically the same as the view displayed by editing.

For the attributes and methods in the class, see the official documentation. In this way, you can write out the classes you need.

Related Article

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.