Solving Cross-Origin Request Issues in Django
Several methods Use django-cors-headers for global control**(recommended)** Use JsonP, only for Get method Set response headers in views.py, can only control a single endpoint django-cors-headers First install pip install django-cors-headers Then just configure it in settings.py INSTALLED_APPS = [ ... 'corshe
目录4 个章节
Several Methods
- Use
django-cors-headersfor global control (recommended) - Use
JsonP, only works with the Get method - Set response headers in
views.py, can only control a single endpoint
django-cors-headers
First, install
pip install django-cors-headers
Then just configure it in settings.py
INSTALLED_APPS = [
...
'corsheaders',
...
]
MIDDLEWARE_CLASSES = (
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware', # 注意顺序
...
)
#跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'*'
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)
That's it.
JsonP
When using Ajax to fetch json data, there are cross-origin restrictions. However, calling js script files on a web page is not affected by cross-origin restrictions, and JSONP takes advantage of this to achieve cross-origin transfer. Therefore, we need to change the dataType in the Ajax call from JSON to JSONP (the corresponding API also needs to support JSONP) format. JSONP can only be used for GET requests. This article will not cover this method; those who need it please search on your own
Configuring Response Headers in Views.py
Modify the implementation function of the corresponding API in views.py to allow other domains to request data via Ajax:
def myview(request):
response = HttpResponse(json.dumps({“key”: “value”, “key2”: “value”}))
response[“Access-Control-Allow-Origin”] = “*”
response[“Access-Control-Allow-Methods”] = “POST, GET, OPTIONS”
response[“Access-Control-Max-Age”] = “1000”
response[“Access-Control-Allow-Headers”] = “*”
return response
_EOF_
Solving Cross-Origin Request Issues in Django
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Django使用django-filer上传图片
前言 (好像不知道写啥) 注意 django-filer目前只支持到django 1.11 setting.py INSTALLED_APPS = [ # image support 'easy_thumbnails', 'filer', 'mptt', ] # Image Upload Support THUMBNAIL_HIGH_RESOLUTION = Tr
2022-05-27 · 0_Django基础知识 · 约 1 分钟
Django清空所有数据和重建表结构
清空所有数据 python manage.py flush 重建表结构 删除数据库中所有表 删除应用文件夹下的migrations目录内所有文件,除了__init__.py 执行以下命令 python manage.py makemigrations python manage.py migrate
2021-12-18 · 0_Django基础知识 · 约 1 分钟
Django应用中的静态文件处理
在日常开发中,我们都是把Django的Debug模式打开,方便调试,在这个模式下,由Django内置的Web服务器提供静态文件服务,不过需要进行一些配置,才能正确访问。 配置settings # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ ST
2021-12-18 · 0_Django基础知识 · 约 1 分钟
2021-11-03:2021年第六届沃网安竞赛-WriteUp-MISC部分
前言 最近好久没更新博客和公众号了,有朋友问是不是在憋大招,但我不好意思说其实是因为最近一段时间太懒了,一直在当咸鱼… 意识到很久没更新这个问题,我是想写点什么的,但好像一直当咸鱼也没啥可分享的,最近刚参加了一个CTF比赛,来分享一些作为CTF小白的解题过程和思路~ 由于篇幅太长,所以本文第一篇就先只发MISC部分,相比较于固定类型的几种题目,MISC是最好玩的,比较考验知识广度和想象力~~(而且
2021-12-18 · CTF · 约 1 分钟
讨论
共 0 条加载评论中…