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
- [Answer]-Django adding an empty label to Choice Field using a ValueListQuery Set for distinct values
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.
- [Answer]-How to manage media files on Windows?
- [Answer]-Django 1.7.1 makemigrations does not recognize an application
- [Answer]-Django class based views, what to override to write to an excel file?
- [Answer]-Django-bootstrap3 fields width control
- [Answer]-Running Django management command from tests?
Source:stackexchange.com