view: CBV模式

url.py文件定义url的方式跟以前的不一样
path("/",view.index.as_view())

view.py
from django.views import View
from django.utils.decorators import method_decorator

继承登入认证模块来进行验证 和view模块

class index(LoginRequiredMixin,View):
def get(request):
pass
def post(request):
pass

def outer(func):
def inner(request, *args, **kwargs):
print(request.method)
return func(request, *args, **kwargs)
return inner

用登入装饰器来装饰一个类,将验证写在outer函数

@method_decorator(outer, name='dispatch')
class index2(View):
def get(request):
pass
@method_decorator(outer)
def dispatch(self, request, *args, **kwargs): #dispach函数会分发到get或者post函数
ret = super(Login, self).dispatch(request, *args, **kwargs)
return ret

原文地址:https://www.cnblogs.com/harveyjie/p/9765865.html