[Fixed]-Runserver can't serve media if MEDIA_URL is within STATIC_URL

9πŸ‘

βœ…

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/'
πŸ‘€Charlesthk

1πŸ‘

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

STATIC_URL = '/static/'
MEDIA_URL = 'static/media/'
if DEBUG:
    MEDIA_URL = 'media/'

0πŸ‘

In Django project keep media folder out of static folder

In your setting.py do like this will help you in production side also when you use whitenoise

if DEBUG:

    STATIC_URL = '/static/'

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

    STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'static')
     ]

else:

   STATIC_URL = '/static/'
   STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

   STATIC_ROOT = BASE_DIR / "staticfiles"

   MEDIA_URL = 'static/media/'
   MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')

    STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'static')
    ]

and run python manage.py collectstatic only in your production server

πŸ‘€Sami

Leave a comment