Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/

like image 375
daino3 Avatar asked Dec 24 '19 13:12

daino3


2 Answers

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/'
like image 99
Charlesthk Avatar answered Nov 12 '22 07:11

Charlesthk


As this is a check in a development environment you could have

STATIC_URL = '/static/'
MEDIA_URL = 'static/media/'
if DEBUG:
    MEDIA_URL = 'media/'
like image 1
Nwawel A Iroume Avatar answered Nov 12 '22 07:11

Nwawel A Iroume