今日学习目标
- 逐步掌握csrf相关装饰器、基于中间件思想编写项目、auth认证模块知识点
学习内容
-
csrf相关装饰器
-
基于中间件思想编写项目
-
auth认证模块
一、 csrf相关装饰器
在平时场景中,并不一定所有的接口验证都需要进行csrf验证,我们采用的是在settings.py中间件配置进行全局配置,如果遇到不需要验证的,我们可以采用局部禁用。
FBV
from django.views.decorators.csrf import csrf_exempt,csrf_protect
@csrf_protect #局部使用
def index(request):
.....
@csrf_exempt #局部禁用
def login(request):
.....
CBV
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.utils.decorators import method_decorator
from django.shortcuts import render, HttpResponse
from django.views import View
class Cs(View):
#@method_decorator(csrf_exempt)
@method_decorator(csrf_protect)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
return HttpResponse('GET,响应内容')
def post(self, request, *args, **kwargs):
return HttpResponse('Post,响应内容')
Django加装饰器
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.utils.decorators import method_decorator