Django CORS Problem

作者: 思考的虫子 | 来源:发表于2019-04-17 11:48 被阅读0次

Python: 3.5
Django: 2.1.5

Problem Desc:
Django works as an API backend, frontend use VUE.
The request was blocked by CORS policy because the header doesn't involve 'Access-Control-Allow-Origin'.

15554720745855.png
  1. install django-cors-headers
pipenv install django-cors-headers
  1. add to settings.py
INSTALLED_APPS = (
    ...
    'corsheaders',
    ...
)
  1. add middleware to settings.py
MIDDLEWARE = [  # Or MIDDLEWARE_CLASSES on Django < 1.10
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]
  1. add your request source to whitelist
# replace with your own frontend server address
CORS_ORIGIN_WHITELIST = (
    '<server address>'
)
  1. restart service, done!

相关文章

网友评论

    本文标题:Django CORS Problem

    本文链接:https://www.haomeiwen.com/subject/xfigwqtx.html