Suziemac Tani
Suziemac Tani

Reputation: 455

Why is django not serving my media files?

In my settings.py,

ROOT_PATH = os.path.dirname(__file__)
#STATICFILES_DIRS =
STATIC_ROOT = os.path.join(ROOT_PATH, 'static')
MEDIA_ROOT = os.path.join(ROOT_PATH, 'media')
STATIC_URL = '/static/'
MEDIA_URL = '/media/

in my template,

     <div class=" event_image">
      <img src="{{ MEDIA_URL }}{{ restaurant.logo }}" /> 
     </div>

This doesnot work in development, it returns a 404 "GET /media/restaurant_detail/restaurant_detail/information_about_object.jpg HTTP/1.1" 404 178672

what am i doing wrong,what is the right way to go about it so it works in both in dev't and production. i looked here (Django 1.4 serving MEDIA_URL and STATIC_URL files on development server) but all in vain.

Upvotes: 1

Views: 3444

Answers (2)

JSalys
JSalys

Reputation: 179

Suziemac, It worked for me too. Here is a link for original documentation: https://docs.djangoproject.com/en/1.10/ref/views/ . When I was developing an app I have used + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT), however this did not work when I deployed an app.

Upvotes: 0

Suziemac Tani
Suziemac Tani

Reputation: 455

Turns out i had to include the urls,my urls

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

)

The above worked..

my project structure
-myproject
-- media
-- static
-- templates
-- settings.py
-- manage.py
-- app

Upvotes: 4

Related Questions