django-cors-headersの設定で軽くハマった話。
django-cors-headers
- インストール方法
pip install django-cors-headers
settingsに下記を追加。
INSTALLED_APPS = [ … 'corsheaders', … ] MIDDLEWARE = [ … 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', … ] CORS_ORIGIN_WHITELIST = ( 'localhost:3000', '127.0.0.1:3000', )
ローカルのreactからAPIを叩くと
“No ‘Access-Control-Allow-Origin’ header is present on the requested resource” in django
と怒られる・・・
CORS_ORIGIN_WHITELISTを下記のようにしたら、解決。
CORS_ORIGIN_WHITELIST = ( 'localhost:3000/', 'localhost:3000', '127.0.0.1:3000/', '127.0.0.1:3000', )