[Django]-ImproperlyConfigured at / Empty static prefix not permitted โ€“ Django

57๐Ÿ‘

โœ…

I added the same line in my urls.py and got the same error as you.

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The documentation here says to use settings.STATIC_URL and settings.STATIC_ROOT

I changed it to the documentation version

urlpatterns = patterns('',
    ....urls......

) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

and the error went away!

I checked my settings file and made sure settings.MEDIA_URL and settings.MEDIA_ROOT were both defined correctly. Later I adjusted urls.py back to using settings.MEDIA_URL and settings.MEDIA_ROOT. Everything worked as expected.

These are the relevant parts of my settings.py file:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
REPOSITORY_ROOT = os.path.dirname(BASE_DIR)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

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

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

I think MEDIA_URL was set incorrectly

๐Ÿ‘คDevin

5๐Ÿ‘

You have too check both of the MEDIA_URL and MEDIA_ROOT as well as for static files STATIC_ROOT STATIC_URL are defined correctly.

Check correct spelling also ๐Ÿ™‚

If one of them is miss configured those will cause this error.

๐Ÿ‘คProjesh Bhoumik

4๐Ÿ‘

To resolve the problem, the following statements must be added to the settings.py file:

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, "media")

3๐Ÿ‘

Make sure that settings.py has:

# Media

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

Then in the urls.py, try this

urlpatterns[
    ...
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)
๐Ÿ‘คAvinash Pandey

2๐Ÿ‘

Just in case you have this issue, ensure you have both MEDIA_URL AND MEDIA_ROOT set.

I was receiving the error

ImproperlyConfigured at / Empty static prefix not permitted

when I only had MEDIA_ROOT set in django 1.11

Alternatively, django project wiki says that it cannot refer to a URL in debug mode:
https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-files-uploaded-by-a-user-during-development

๐Ÿ‘คMykel

2๐Ÿ‘

I am following Django 2.2 & Python | The Ultimate Web Development Bootcamp my issue was that I have forgot to declare these to portfolio-project/portfolio/settings.py/ to the bottom area:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/Media/'
๐Ÿ‘คsogu

1๐Ÿ‘

I had recently got the same error when working with Django 2.1 The problem was I did not specify explicitly MEDIA_URL = '/media/' in the project settings file. Once I declared the same the error went away.

๐Ÿ‘คTejasPancholi

0๐Ÿ‘

To fix this error, I had to put STATIC_ROOT and STATIC_URL above the STATICFILES_DIRS declaration.

๐Ÿ‘คehacinom

-1๐Ÿ‘

Fix this error by adding this line in urls.py

urlpatterns = [
 ....
]  + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
๐Ÿ‘คAshok

Leave a comment