浏览代码

feat(api): add CORS headers to allow external usage, see #87

Peter Thomassen 6 年之前
父节点
当前提交
25c6d50b57
共有 2 个文件被更改,包括 7 次插入0 次删除
  1. 6 0
      api/api/settings.py
  2. 1 0
      api/requirements.txt

+ 6 - 0
api/api/settings.py

@@ -41,9 +41,11 @@ INSTALLED_APPS = (
     'rest_framework',
     'rest_framework',
     'djoser',
     'djoser',
     'desecapi',
     'desecapi',
+    'corsheaders',
 )
 )
 
 
 MIDDLEWARE = (
 MIDDLEWARE = (
+    'corsheaders.middleware.CorsMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
 )
 )
@@ -107,6 +109,10 @@ DJOSER = {
     'TOKEN_MODEL': 'desecapi.models.Token',
     'TOKEN_MODEL': 'desecapi.models.Token',
 }
 }
 
 
+# CORS
+# No need to add Authorization to CORS_ALLOW_HEADERS (included by default)
+CORS_ORIGIN_ALLOW_ALL=True
+
 TEMPLATES = [
 TEMPLATES = [
     {
     {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
         'BACKEND': 'django.template.backends.django.DjangoTemplates',

+ 1 - 0
api/requirements.txt

@@ -1,5 +1,6 @@
 coverage~=4.5.3
 coverage~=4.5.3
 Django~=2.2.0
 Django~=2.2.0
+django-cors-headers~=3.0.2
 django-nocaptcha-recaptcha==0.0.20  # updated manually
 django-nocaptcha-recaptcha==0.0.20  # updated manually
 djangorestframework~=3.9.3
 djangorestframework~=3.9.3
 djangorestframework-bulk~=0.2.0
 djangorestframework-bulk~=0.2.0