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),
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 .
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()),
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 .
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']