程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Understanding of djangofbv and CBV

編輯:Python

1.FBV( Function based view )

        FBV(function based views), A function-based view .FBV Simple and easy to understand , But it's hard to reuse . They can't be like CBV That way, you can inherit from the parent class .FBV The guidelines for writing require us to : The less view code the better ; View code cannot be repeated ; Views should only handle rendering logic . Business logic should be placed in the data model as much as possible , Or in the form object ; Keep the view code simple ; Use them to write custom 403, 404, 500 Wait for the error handler ; Avoid using nested if block .CBV yes View Class ; The main difference here is that the request is based on HTTP Method named class method , for example GET ,POST ,PUT ,HEAD etc. . therefore , ad locum , We do not need to make a condition to judge that the request is a POST Or is it a GET . The code will jump directly to the correct method , stay View Class handles this logic internally .

from django.shortcuts import render, redirect
def login(request):
if request.method == "GET":
"""
Handle GET request
"""
return render(request, 'login.html')
if request.method == "GET":
"""
Handle POST request
"""
user = request.POST.get('username')
pwd = request.POST.get('password')
if username == 'tony' and password == "123456":
request.session['user'] = user
return redirect('/index/')
return render(request, 'login.html')
# urls.py in
url(r'^login/$', views.login),

 

 2.CBV( Class based view )

        CBV Is a class based view , Is to use the class to handle the user's request , We can use different methods to deal with different requests in our class , This greatly improves the readability of the code .CBV Allows us to use different instance methods to respond to different HTTP Request method , Not like it FBV Use conditional branch code like that . But the defined class should inherit the parent class View, So we're using CBV You need to import and stock in in advance :from django.views import View

         Priority will be given to the execution of the method corresponding to the request dispatch Method ( stay get/post/put… Before the method ),dispatch() Method will call corresponding methods according to different requests to handle . Actually , We all know what we have learned before Django Of url A request is assigned to a callable function , Not a class , How do you implement a class based view ? Mainly through the parent class View A static method provided by as_view() ,as_view Method is an external interface based on class , He returns a view function , After the call, the request is passed to dispatch Method ,dispatch Method then processes different methods according to different requests .

  • cbv When defining a class, you must inherit view;
  • Writing url You must add as_view();
from django.shortcuts import render,HttpResponse
from django.views import View
class LoginView(View):
def get(self, request):
"""
Handle GET request
"""
return render(request, 'login.html')
def post(self, request):
"""
Handle POST request
"""
user = request.POST.get('username')
pwd = request.POST.get('password')
if username == 'tony' and password == "123456":
request.session['user'] = user
return redirect('/index/')
return render(request, 'login.html')
# urls.py in
url(r'^user/$', views.LoginView.as_view()),

 

3.CBV Model advantage

        CBV The logic of the entire view function is divided into multiple functions under the class , Rely on function calls to implement complete logic ; Improve code reusability , More flexible , Let developers use object-oriented technology , For example, more inheritance 、 Polymorphism etc. ; You can use different functions for different HTTP Method treatment , Not through a lot if Judge , Improve code readability .

4. Be careful :

         When we use CBV When the way , First of all, pay attention to urls.py To be written in the document “ Class name .as_view()” Mode mapping , Second, in the class we define get/post Methods the names of these methods are not defined by us , But in a fixed pattern ,View Class supports the following methods :

http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved