29

I'm working on uploading/displaying images with Django.

The website is deployed on Heroku.

Following this tutorial I was able to successfully upload images.

However, the images weren't being displayed in the template.

I then learned that my urls.py should have this line at the end:

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

I added this to the end of my urls.py but now I'm getting this error:

ImproperlyConfigured at / Empty static prefix not permitted

I have MEDIA_URL and MEDIA_ROOT in my settings.py and neither are empty.

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

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

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

Why is this error happening and how would I fix it?

Here's what I think is the relevant part of my urls.py:

from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
import notifications
admin.autodiscover()

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

) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Daniel Scott
  • 1,867
  • 3
  • 13
  • 18
  • ...what is your `MEDIA_URL` and `MEDIA_ROOT`? You said they're defined; but what are they? – rnevius May 26 '14 at 16:21
  • 3
    `static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)` is for development only, and only works with `debug` set to `True`. https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user-during-development – rnevius May 26 '14 at 17:04

9 Answers9

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
  • 1,262
  • 15
  • 20
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
  • 1,058
  • 14
  • 17
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")
Powertieke
  • 2,368
  • 1
  • 14
  • 21
Antonio José
  • 473
  • 2
  • 5
  • 14
  • Welcome to SO and thank you for contributing. It would help others and the OP understanding your solution if you'd add more specific explanations (e.g. background information, what does it do, why does it help with the given problem). – Selaron Jan 31 '19 at 21:19
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)
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
  • 1,355
  • 15
  • 25
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
  • 2,738
  • 5
  • 31
  • 90
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
  • 1,231
  • 1
  • 7
  • 4
0

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

ehacinom
  • 8,070
  • 7
  • 43
  • 65
-1

Fix this error by adding this line in urls.py

urlpatterns = [
 ....
]  + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Ashok
  • 19
  • 4