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/

daino3
  • 4,386
  • 37
  • 48
  • 2
    If it's not explained in the release notes, you'll probably have better luck asking the people who made the change. – ryan28561 Dec 24 '19 at 16:10
  • This is a check that only happens if you are in dev mode as described in this ticket (and the referenced pull request: https://code.djangoproject.com/ticket/29570 – gluejar Aug 25 '20 at 22:15

3 Answers3

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
  • 9,394
  • 5
  • 43
  • 45
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/'
Nwawel A Iroume
  • 1,249
  • 3
  • 21
  • 42
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
  • 95
  • 2
  • 6