From 53bb844f26f071ca563df9f9213646abb7511a45 Mon Sep 17 00:00:00 2001 From: Benjamin Webb Date: Fri, 15 Aug 2025 16:36:45 -0400 Subject: [PATCH 1/2] Add support for CORS with Django --- pygeoapi/django_/settings.py | 7 +++++++ requirements-django.txt | 1 + 2 files changed, 8 insertions(+) diff --git a/pygeoapi/django_/settings.py b/pygeoapi/django_/settings.py index 9a74fef90..b87d3a73a 100644 --- a/pygeoapi/django_/settings.py +++ b/pygeoapi/django_/settings.py @@ -175,3 +175,10 @@ # Defaults to True in Django # https://docs.djangoproject.com/en/3.2/ref/settings/#append-slash APPEND_SLASH = not API_RULES.strict_slashes + +# CORS: optionally enable from config. +if PYGEOAPI_CONFIG['server'].get('cors', False): + INSTALLED_APPS.append('corsheaders') + MIDDLEWARE.append('corsheaders.middleware.CorsMiddleware') + + CORS_ORIGIN_ALLOW_ALL = True diff --git a/requirements-django.txt b/requirements-django.txt index ef3e3ec82..92d78d376 100644 --- a/requirements-django.txt +++ b/requirements-django.txt @@ -1,2 +1,3 @@ Django +django-cors-headers pydantic From 1ee9ae7e51e9b102bcb46a3514c3a8887a81fa17 Mon Sep 17 00:00:00 2001 From: Benjamin Webb Date: Fri, 15 Aug 2025 16:45:27 -0400 Subject: [PATCH 2/2] Update settings.py per https://pypi.org/project/django-cors-headers/ --- pygeoapi/django_/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygeoapi/django_/settings.py b/pygeoapi/django_/settings.py index b87d3a73a..53dad2efa 100644 --- a/pygeoapi/django_/settings.py +++ b/pygeoapi/django_/settings.py @@ -179,6 +179,6 @@ # CORS: optionally enable from config. if PYGEOAPI_CONFIG['server'].get('cors', False): INSTALLED_APPS.append('corsheaders') - MIDDLEWARE.append('corsheaders.middleware.CorsMiddleware') + MIDDLEWARE.insert(0, 'corsheaders.middleware.CorsMiddleware') CORS_ORIGIN_ALLOW_ALL = True