今日学习目标
- 逐步掌握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
# 1.方法

本文介绍了Django框架中关于csrf相关装饰器的使用,如何基于中间件思想编写项目,以及深入探讨了auth认证模块,包括authenticate(), login(), is_authenticated(), login_required()等关键方法的运用。"
111711922,10296077,深覆盖与深覆合:区分、原因及矫正方法解析,"['口腔健康', '牙齿矫正', '咬合问题', '正畸', '错颌畸形']
691

被折叠的 条评论
为什么被折叠?



