diff options
| author | Oleg Kainov <kainov.oleg@gmail.com> | 2018-12-05 16:15:33 -0800 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-09-25 19:47:03 +0200 |
| commit | c574bec0929cd2527268c96a492d25223a9fd576 (patch) | |
| tree | 80c642eca0ca64d63d1596bc237465685c92ce5c /django/conf/__init__.py | |
| parent | 580e644f24f1c5ae5b94784fb73a9953a178fd26 (diff) | |
Fixed #25598 -- Added SCRIPT_NAME prefix to STATIC_URL and MEDIA_URL set to relative paths.
Thanks Florian Apolloner for reviews.
Co-authored-by: Joel Dunham <Joel.Dunham@technicalsafetybc.ca>
Diffstat (limited to 'django/conf/__init__.py')
| -rw-r--r-- | django/conf/__init__.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/django/conf/__init__.py b/django/conf/__init__.py index b32e56184d..ec7efadf46 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -15,7 +15,8 @@ from pathlib import Path import django from django.conf import global_settings -from django.core.exceptions import ImproperlyConfigured +from django.core.exceptions import ImproperlyConfigured, ValidationError +from django.core.validators import URLValidator from django.utils.deprecation import RemovedInDjango40Warning from django.utils.functional import LazyObject, empty @@ -109,6 +110,26 @@ class LazySettings(LazyObject): setattr(holder, name, value) self._wrapped = holder + @staticmethod + def _add_script_prefix(value): + """ + Add SCRIPT_NAME prefix to relative paths. + + Useful when the app is being served at a subpath and manually prefixing + subpath to STATIC_URL and MEDIA_URL in settings is inconvenient. + """ + # Don't apply prefix to valid URLs. + try: + URLValidator()(value) + return value + except (ValidationError, AttributeError): + pass + # Don't apply prefix to absolute paths. + if value.startswith('/'): + return value + from django.urls import get_script_prefix + return '%s%s' % (get_script_prefix(), value) + @property def configured(self): """Return True if the settings have already been configured.""" @@ -128,6 +149,14 @@ class LazySettings(LazyObject): ) return self.__getattr__('PASSWORD_RESET_TIMEOUT_DAYS') + @property + def STATIC_URL(self): + return self._add_script_prefix(self.__getattr__('STATIC_URL')) + + @property + def MEDIA_URL(self): + return self._add_script_prefix(self.__getattr__('MEDIA_URL')) + class Settings: def __init__(self, settings_module): |
