runserver can't serve media if MEDIA_URL is within STATIC_URL
Asked Answered
A

3

13

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/

Antitoxic answered 24/12, 2019 at 13:32 Comment(2)
If it's not explained in the release notes, you'll probably have better luck asking the people who made the change.Logarithm
This is a check that only happens if you are in dev mode as described in this ticket (and the referenced pull request: code.djangoproject.com/ticket/29570Isolecithal
R
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/'
Rattrap answered 26/8, 2020 at 8:44 Comment(1)
I can't test pwaLangham
G
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/'
Gibson answered 2/5, 2022 at 15:26 Comment(0)
H
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

Hail answered 19/8, 2023 at 13:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.