From fc91c52baff78db5c8892e898824664931c759da Mon Sep 17 00:00:00 2001 From: FriggemannMichael Date: Tue, 7 Jul 2026 10:46:33 +0200 Subject: [PATCH] Support sub-path deployment and add gunicorn - Make STATIC_URL and FORCE_SCRIPT_NAME environment-driven so the app can be served under a sub-path (e.g. /backend) with correct admin, static and reverse()-generated URLs. - Build the offer-detail link with reverse() so it honours FORCE_SCRIPT_NAME instead of a hard-coded /api/ path. - Add gunicorn to requirements for the production WSGI server. --- core/settings.py | 5 ++++- offers_app/api/serializers.py | 3 ++- requirements.txt | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/core/settings.py b/core/settings.py index 2af211a..cd14123 100644 --- a/core/settings.py +++ b/core/settings.py @@ -168,13 +168,16 @@ def _env_list(name, default=''): # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/6.0/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = os.environ.get('DJANGO_STATIC_URL', 'static/') STATIC_ROOT = BASE_DIR / 'staticfiles' # Deployment behind a reverse proxy (Nginx/Cloudflare) that terminates HTTPS. +# FORCE_SCRIPT_NAME lets the app be served under a sub-path (e.g. /backend) so +# reverse()-generated URLs include that prefix. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') +FORCE_SCRIPT_NAME = os.environ.get('DJANGO_FORCE_SCRIPT_NAME') or None if not DEBUG: SESSION_COOKIE_SECURE = True diff --git a/offers_app/api/serializers.py b/offers_app/api/serializers.py index 46812df..85df0e6 100644 --- a/offers_app/api/serializers.py +++ b/offers_app/api/serializers.py @@ -1,3 +1,4 @@ +from django.urls import reverse from rest_framework import serializers from offers_app.models import Offer, OfferDetail @@ -32,7 +33,7 @@ class Meta: ] def get_url(self, obj): - return f'/api/offerdetails/{obj.id}/' + return reverse('offerdetail-detail', kwargs={'pk': obj.id}) class UserDetailsSerializer(serializers.Serializer): diff --git a/requirements.txt b/requirements.txt index e9bb38c..259038d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ django-stubs==6.0.6 django-stubs-ext==6.0.6 djangorestframework==3.17.1 djangorestframework-stubs==3.17.0 +gunicorn==26.0.0 iniconfig==2.3.0 packaging==26.2 Pillow==12.2.0