前因
drf 前后端分离 因为是跨域访问所以不存在csrf 验证
一、前端设置(代理ip)
不同前端框架会有不同的设置,所以此处会在具体项目中介绍
二、服务器设置
https://github.com/ottoyiu/django-cors-headers
- Setup
Install from pip:
pip install django-cors-headers
and then add it to your installed apps:
- settings
INSTALLED_APPS = (
...
'corsheaders',
...
)
You will also need to add a middleware class to listen in on responses:
MIDDLEWARE = [ # Or MIDDLEWARE_CLASSES on Django < 1.10
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
CORS_ORIGIN_WHITELIST = (
'google.com',
'hostname.example.com',
'localhost:8000',
'127.0.0.1:9000'
)