'runserver can't serve media if MEDIA_URL is within STATIC_URL
My config is as follows:
STATIC_URL = '/static/'
MEDIA_URL = '/static/media/'
And since I upgraded django 2.1 to 2.2 I get:
"runserver can't serve media if MEDIA_URL is within STATIC_URL."
django.core.exceptions.ImproperlyConfigured: runserver can't serve media if MEDIA_URL is within STATIC_URL.
I understand the error. My question is "why not"? There are very valid reasons you'd want media as a subdirectory to static.
Also, there's zero mention of this breaking change in the 2.2 release notes: https://docs.djangoproject.com/en/3.0/releases/2.2/
Solution 1:[1]
This warning has been done in response to this ticket #29570: Add check that MEDIA_URL is not inside STATIC_URL..
Also quoting #15199: Allow MEDIA_ROOT inside STATIC_ROOT
After further IRC discussion with jezdez, closing this wontfix. Supporting a configuration with MEDIA_ROOT inside STATIC_ROOT introduces a number of additional complexities and couplings between staticfiles and the MEDIA_* settings, which we are trying to avoid, and it's not clear what meaningful benefits it buys us. The main mentioned benefit was to only require one alias on the front-end webserver: that seems minor, since an alias is e.g. just one line in an nginx conf file. In any case, the same result can be achieved by putting MEDIA_ROOT and STATIC_ROOT side by side in a parent directory, and aliasing the front-end webserver to that parent directory.
So basically, you could to :
STATIC_URL = '/static/static/'
MEDIA_URL = '/static/media/'
Solution 2:[2]
As this is a check in a development environment you could have
STATIC_URL = '/static/'
MEDIA_URL = 'static/media/'
if DEBUG:
MEDIA_URL = 'media/'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Charlesthk |
Solution 2 | Nwawel A Iroume |