[RFC PATCH v2 05/19] settings: Enable django-cors-headers

Stephen Finucane stephen at that.guru
Thu Sep 2 02:57:42 AEST 2021


This is proving very useful as I attempt to add some Vue.js components
to the frontend.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/settings/dev.py | 26 +++++++++++++++++++++++---
 requirements-dev.txt      |  1 +
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git patchwork/settings/dev.py patchwork/settings/dev.py
index aa3ee3c5..62ae7004 100644
--- patchwork/settings/dev.py
+++ patchwork/settings/dev.py
@@ -19,6 +19,11 @@ try:
 except ImportError:
     debug_toolbar = None
 
+try:
+    import corsheaders
+except ImportError:
+    corsheaders = None
+
 #
 # Core settings
 # https://docs.djangoproject.com/en/2.2/ref/settings/#core-settings
@@ -81,13 +86,13 @@ if debug_toolbar:
         'debug_toolbar'
     ]
 
-    DEBUG_TOOLBAR_PATCH_SETTINGS = False
-
-# This should go first in the middleware classes
+    # This should go as high as possible in the middleware classes
     MIDDLEWARE = [
         'debug_toolbar.middleware.DebugToolbarMiddleware',
     ] + MIDDLEWARE
 
+    DEBUG_TOOLBAR_PATCH_SETTINGS = False
+
     INTERNAL_IPS = [
         '127.0.0.1', '::1',
         '172.18.0.1'
@@ -102,6 +107,21 @@ if dbbackup:
 
     DBBACKUP_STORAGE_OPTIONS = {'location': '.backups'}
 
+# django-cors-headers
+
+if corsheaders:
+    INSTALLED_APPS += [
+        'corsheaders',
+    ]
+
+    # This should go as high as possible in the middleware classes
+    MIDDLEWARE = [
+        'corsheaders.middleware.CorsMiddleware',
+    ] + MIDDLEWARE
+
+    CORS_ORIGIN_ALLOW_ALL = True
+    CORS_EXPOSE_HEADERS = ['Link']
+
 #
 # Patchwork settings
 #
diff --git requirements-dev.txt requirements-dev.txt
index d58dfaa9..229ed0c2 100644
--- requirements-dev.txt
+++ requirements-dev.txt
@@ -3,4 +3,5 @@ djangorestframework~=3.12.0
 django-filter~=2.4.0
 django-debug-toolbar~=3.2.0
 django-dbbackup~=3.3.0
+django-cors-headers~=3.2.0
 -r requirements-test.txt
-- 
2.31.1



More information about the Patchwork mailing list