0

this is my static and media settings in setting.py

STATIC_URL = '/static/'
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')

And in my project folder structures, static and media folders are next to each other.

So when I use this html tag

<img src="/static/pic.jpg">

Or http://127.0.0.1:8000/static/pic.jpg, it works.

But <img src="/media/pic.jpg"> and http://127.0.0.1:8000/media/pic.jpg doesn't.

How is that Django recognizes 127.0.0.1:8000/static/ as a valid address But it throws Page not found (404) when I'm trying 127.0.0.1:8000/media/ ?

This is my url config

from django.conf.urls import patterns, include, url
from django.contrib import admin
from mysite.views import *

urlpatterns = patterns('',
    url(r'^$', 'mysite.views.home', name='home'),
    url(r'^home/', 'mysite.views.gohome'),
    url(r'^admin/', include(admin.site.urls)),
)
Ghasem
  • 14,455
  • 21
  • 138
  • 171

2 Answers2

1

You need to add this to your urls.py file

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

So, It should looks like this:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from mysite.views import *

urlpatterns = patterns('',
    url(r'^$', 'mysite.views.home', name='home'),
    url(r'^home/', 'mysite.views.gohome'),
    url(r'^admin/', include(admin.site.urls)),
)

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

With this you can serve the static media from Django when DEBUG=True (when you are on local computer) but you can let your web server configuration serve static media when you go to production and DEBUG=False

levi
  • 22,001
  • 7
  • 73
  • 74
  • Thank you. it does the job with DEBUG=True. But how to make it work on the server? – Ghasem Feb 04 '15 at 18:34
  • @AlexJolig that depends on what app server(gunicorn/apache) you will use in production, or if you will have a server to serve static/media files as nginx. – levi Feb 04 '15 at 18:37
  • I'm using nginx in local and on the server – Ghasem Feb 04 '15 at 18:37
  • @AlexJolig you can check http://stackoverflow.com/questions/8370658/how-to-serve-django-media-files-via-nginx also, there is a lot of question in SO about that topic. If you aren't happy with existing answers, try to open a new question. – levi Feb 04 '15 at 18:39
  • Thank you. I added media location in nginx configurations and it works now. – Ghasem Feb 04 '15 at 18:43
0

Alternatively to Levi's answer, you can do:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

...
urlpatterns += staticfiles_urlpatterns()

You need to set STATICFILES_DIRS in your settings. https://docs.djangoproject.com/en/1.7/ref/contrib/staticfiles/#django.contrib.staticfiles.urls.staticfiles_urlpatterns

The other alternative is:

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

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

I like this last one the most because it's obvious what happens.

Both only serve when DEBUG is True.

allcaps
  • 10,945
  • 1
  • 33
  • 54