Anshdeep singh
Anshdeep singh

Reputation: 63

Unable to view media files in django

I have correctly set the settings for media in settings.py as:-

MEDIA_URL = '/media/'

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

and also specified the correct url settings

if settings.DEBUG:
urlpatterns += patterns(
    'django.views.static',
    (r'^media/(?P<path>.*)',
    'serve',
    {'document_root': settings.MEDIA_ROOT}), )

Whenever i upload an image through admin..the image gets uploaded in my media folder but i can't see it when i run my development server...it shows a page not found (404) error...

I think the problem is due to some permissions of uploaded files which is why django shows a 404 page when trying to access them..if someone knows about this please help

Please suggest me a solution Thank you!

Upvotes: 1

Views: 2597

Answers (2)

Othman
Othman

Reputation: 3018

Do this


  from django.conf.urls.static import static
  from django.conf import settings

  urlpatterns = patterns('',

  # your urls here. 

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

Upvotes: 1

Jorick Spitzen
Jorick Spitzen

Reputation: 1639

Try this:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Relevant url:

https://docs.djangoproject.com/en/1.7/howto/static-files/#serving-files-uploaded-by-a-user-during-development

Upvotes: 3

Related Questions