@csrf_exempt
在Django中对于基于函数的视图我们可以 @csrf_exempt 注解来标识一个视图可以被跨域访问。那么对于基于类的视图,我们应该怎么办呢?
简单来说可以有两种访问来解决
方法一:在类的 dispatch 方法上使用 @csrf_exempt
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'),
]
在views.py中
from django.views.generic.base import View
class ueditor(View):
@csrf_exempt
def post(self, request):
print(request.POST.get('abc', ""))
return render(request, "ueditor.html")
这样写是csrf_exempt是不会生效的
只有继承了django rest framework的apiview有才用:
from rest_framework.views import APIView
class ueditor(APIView):
@csrf_exempt
def post(self, request):
print(request.POST.get('abc', ""))
return render(request, "ueditor.html")
@csrf_exempt的更多相关文章
- 11 Django REST Framework 针对基于类的视图添加 @csrf_exempt
01-在类的 dispatch 方法上使用 @csrf_exempt from django.views.decorators.csrf import csrf_exempt class MyView ...
- python 全栈开发,Day88(csrf_exempt,ES6 快速入门,Vue)
BBS项目内容回顾 1. 登陆页面 1. 验证码 1. PIL(Pillow) 2. io 2. ORM 1. 增删改查 3. AJAX $.ajax({ url: '', type: '', dat ...
- django中CBV加csrf_exempt函数问题
CSRF Token相关装饰器在CBV只能加到dispatch方法上 备注: 1. csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件. 2. c ...
- Django @csrf_exempt不适用于基于通用视图的类(Django @csrf_exempt does not work on generic view based class)
class ChromeLoginView(View): def get(self, request): return JsonResponse({'status': request.user.is_ ...
- Django @csrf_exempt不能在类视图中工作(Django @csrf_exempt not working in class View)
我在Django 1.9中有一个使用SessionMiddleware的应用程序.我想在同一个项目中为这个应用程序创建一个API,但是在做一个POST请求时,它不能使用@csrf_exempt注释. ...
- django之csrf_exempt解决跨域请求的问题
一: from django.views.decorators.csrf import csrf_exempt # 获取微信返回的code信息 @csrf_exempt def wechat_auth ...
- Django
一.Django 简介 Django 是一个由 Python 写成的开放源代码的 Web 应用框架.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是 CMS(内容管理系统) ...
- django之一些feature
前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. cookie cookie 是一种发送到客 ...
- 前端之ajax
前端之ajax 本节内容 ajax介绍 原生js实现ajax jquery实现ajax json 跨域请求 1. ajax介绍 AJAX(Asynchronous Javascript And XML ...
- javascript的ajax
AJAX 一 AJAX预备知识:json进阶 1.1 什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javas ...
随机推荐
- STM32 系统初始化
#include "system.h" void system_init(void){ //系统中断设置,抢占优先级0~15,无子优先级 NVIC_PriorityGroupCon ...
- OSPF v3与v2的区别
- SpringBoot笔记--自动配置(高级内容)(中集)
@Enable*注解 使用该注解,需要导入相应的依赖坐标,其中的groupId标签里面写入Bean的Java文件所在的包的路径下面 spring-enable-other 还需要在SpringBoot ...
- 在CentOS中安装Docker
概述 Docker是一款使用Golang开发的开源容器引擎,我们可以使用Docker将自己的应用和相关依赖进行打包,实现在不同服务器上进行快速部署,而不需要再更多关注部署环境的差异性.结合kubern ...
- 【数据结构与算法学习】散列表(Hash Table,哈希表)
实现语言:C++ 1. 散列表 散列表,英文名称为Hash Table,又称哈希表.杂凑表等. 线性表和树表的查找是通过比较关键字的方法,查找的效率取决于关键字的比较次数. 而散列表是根据关键字直接访 ...
- Why WebRTC丨“浅入深出”的工作原理详解
前言 近几年实时音视频通信应用呈现出了大爆发的趋势.在这些实时通信技术的背后,有一项不得不提的技术--WebRTC. 今年 1 月,WebRTC 被 W3C 和 IETF 发布为正式标准.据调研机构 ...
- Java/Kotlin 使用Redis模拟发送验证码
原文地址: Java/Kotlin 使用Redis模拟发送邮件验证码 - Stars-One的杂货小窝 Java中常用语连接Redis的库有lettuce和jredis,一般是推荐lettuce,其具 ...
- 免费1年服务器,部署个ChatGPT专属网页版
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 白皮袄个免费1年服务器,部署个ChatGPT专属网页版! api.openai.com por ...
- Java------常用类(二)
import org.junit.Test; import java.io.UnsupportedEncodingException; import java.util.Arrays; /** * 涉 ...
- 部署lnmp环境,安装typecho博客
安装nginx和PHP环境 root@cby:~# apt install nginx php7.4 php7.4-mysql php7.4-fpm 修改nginx配置文件 root@cby:~# v ...