前因
drf 前后端分離 因為是跨域訪問所以不存在csrf 驗證
一、前端設(shè)置(代理ip)
不同前端框架會有不同的設(shè)置,所以此處會在具體項目中介紹
二、服務(wù)器設(shè)置
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'
)