[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>在正式环境下,不关闭csrf中间件,自定义跳过csrf检测的post请求
<>两种方法
<>第一种:一劳永逸,随意删除和添加要通过的post请求
比较适合大量的通过csrf的请求
1.在资项目中创建middleware.py文件:
2.在middleware.py文件中写入以下代码:
将需要跳过csrf检测的post请求,加入到URL_LIST 列表里即可
import re from django.utils.deprecation import MiddlewareMixin class
IgnoreCrsfMiddleware(MiddlewareMixin): def process_request(self, request, **karg
): # 哪个post请求需要跳过csrf检测,加入到URL_LIST 列表里即可 URL_LIST = [r'^/test_csrf/$', r
'^/c/d/$'] for u in URL_LIST: if re.match(u, request.path): request.
csrf_processing_done= True
3.在settings.py的中间件中加入自定义的中间件:
这样保证post请求成功通过csrf检测
<>第二种:针对指定的view
<>三种形式都可以:
都是导入的这个包:
from django.views.decorators.csrf import csrf_exempt
<>第一种:普通的视图函数
# 获取微信返回的code信息 @csrf_exempt def wechat_auth(req): if req.method == 'POST':
code = req.POST.get('code') data_info = get_access_token_info(code) return
JsonResponse({'message': data_info, "status": '1'}) return
JsonResponse({'message': '扫码失败,请刷新重试!',"status": 0})
<>第二种:继承视图类的类视图
from django.views.decorators.csrf import csrf_exempt class MyView(View): def
get(self, request): return HttpResponse("hi") def post(self, request): return
HttpResponse("hi") @csrf_exempt def dispatch(self, *args, **kwargs): return
super(MyView, self).dispatch(*args, **kwargs)
<>第三种:在urls.py中设置
from django.conf.urls import url from django.views.decorators.csrf import
csrf_exempt import views urlpatterns = [ url(r'^myview/$',
csrf_exempt(views.MyView.as_view()), name='myview'), ]
三种形式,选一种即可.