CBV和FBV
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CBV和FBV相关的知识,希望对你有一定的参考价值。
from django.shortcuts import render,HttpResponse
from django.views import View
# Create your views here.
class LoginView(View):
#form表单提交请求只能是get post
#ajax提交请求 get post获取 put修改 patch修改 delete
# def dispatch(self, request, *args, **kwargs):
# print(‘dispath start ‘)
# moethod=request.method.lower()
# func=getattr(self,moethod)
# print(‘dispath end ‘)
# return func(request,*args,**kwargs)
‘‘‘
dispath核心代码
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)
‘‘‘
def dispatch(self, request, *args, **kwargs):
reponse=super(LoginView,self).dispatch(request, *args, **kwargs)
return reponse
def get(self,request,*args,**kwargs):
print(‘get‘)
return render(request,‘login.html‘)
def post(self,request,*args,**kwargs):
print(‘posrt‘)
return HttpResponse(‘你好‘)
以上是关于CBV和FBV的主要内容,如果未能解决你的问题,请参考以下文章