Python攻城师的成长————Django框架(csrf相关装饰器、基于中间件思想编写项目、auth认证模块)

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

今日学习目标

  • 逐步掌握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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值