FBV: function basic view
def home(request): return HttpResponse()
CBV: class basic view
class HomeView(View): def get(self, request): return HttpResponse()
Django中间件的五个方法方法
执行顺序:请求过来
Django的crsf验证是在中间件CsrfViewMiddleware中进行验证的,具体来讲是在此中间件的process_view中验证的。那么为什么要在process_view中验证而不是在process_request中呢?我们先了解一个情况,如果我不希望某个路由函数进行csrf验证,可以加上csrf_exempt,免除csrf验证,所有必须在process_view中验证,而不是在process_request中。
from django.views.decorators.csrf import csrf_exempt@csrf_exemptdef home(request): return HttpResponse()
如果是在CBV中要取消某个路由的csrf验证呢?
from django.view.decorators.csrf import csrf_exemptfrom django.utils.decorators import method_decoratorclass HomeView(View): @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): return super().dispatch(request, *args, **kwargs) def post(self, request): return HttpResponse()或者@method_decorator(csrf_exempt, name="dispatch")class HomeView(View): pass
必须使用method_decorator装饰器修饰,并且只能添加到dispatch中,加到单独的函数中是不行的。
BaseAuthentication.authenticate方法返回值:None: 执行下一个认证非None: 验证通过抛出异常:验证不通过
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!