Django中免除csrf校验有几种方式,怎样做
Admin 2022-06-16 群英技术资讯 729 次浏览
在django中默认启动csrf校验,当用户发起post请求时,必须携带csrf_token参数。如果不想使用csrf校验时,可以使用以下方式免除校验。以下方式都是在局部中使用,如果想全局禁用时,需要在settings文件中配置,这种方式不推荐使用。
from django.views.decorators.csrf import csrf_exempt# 免除csrf校验@csrf_exempt def users(request): uses_list = ["柚子", "西瓜"] return HttpResponse(json.dumps(uses_list))
# dispatch是类视图的根方法,通过dispatch进行反射找到其他请求 from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator class StudentsView(View): """student view""" @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): print("before") ret = super(StudentsView, self).dispatch(request, *args, **kwargs) print("after") return ret(request, *args, **kwargs) def get(self,*args,**kwargs): return HttpResponse("get") def post(self,*args,**kwargs): return HttpResponse("post") def put(self,*args,**kwargs): return HttpResponse("put") def delete(self,*args,**kwargs): return HttpResponse("delete")
from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator @method_decorator(csrf_exempt,name="dispatch") class StudentsView(View): """student view""" def get(self,*args,**kwargs): return HttpResponse("get")
from django.views.decorators.csrf import csrf_exempt class MyBaseView(object): @csrf_exempt def dispatch(self, request, *args, **kwargs): print("before") ret = super(MyBaseView, self).dispatch(request, *args, **kwargs) print("after") return ret
from django.views.decorators.csrf import csrf_exempt urlpatterns = [ path('teachers/', csrf_exempt(TeachersView.as_view()), name="teachers"), ]
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
大家都知道concat()函数可以沿着一条轴将多个对象进行堆叠,其使用方式类似数据库中的数据表合并,在使用merge()函数进行合并时,默认会使用重叠的列索引做为合并键,即取行索引重叠的部分,本文给大家介绍python 数据合并concat函数与merge函数,感兴趣的朋友一起看看吧
这篇文章主要介绍了Python pyecharts绘制词云图代码,
本篇文章给大家带来了关于Python的相关知识,numpy里有一个非常神奇的函数叫做np.where()函数,下面这篇文章主要给大家介绍了关于Python np.where()的详解以及代码应用的相关资料,文中通过实例代码介绍的非常详细,希望对大家有帮助。
Python使用shutil操作文件、subprocess运行子程序,一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
python@运算符是什么意思?怎样使用?对于刚接触Python的朋友,可能对@运算符不是很了解,因此这篇文章就给大家介绍一下python@运算符的内容,感兴趣的朋友就继续往下看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008