django static files not working
In development:
STATICFILES_DIRS
should have all static directories inside which all static files are residentSTATIC_URL
should be/static/
if your files are in the local machine otherwise put the base URL here e.g. "http://example.com/"INSTALLED_APPS
should include'django.contrib.staticfiles'
In the template, load the staticfiles module:
{% load staticfiles %}
..
..
<img src='{% static "images/test.png" %}' alt='img' />
In Production:
Add
STATIC_ROOT
that is used by Django to collect all static files fromSTATICFILES_DIRS
to itCollect static files
python manage.py collectstatic [--noinput]
- add the path to urls.py
from . import settings
..
..
urlpatterns = patterns('',
..
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATIC_ROOT)}),)`
More detailed articles are listed below:
http://blog.xjtian.com/post/52685286308/serving-static-files-in-django-more-complicated
http://agiliq.com/blog/2013/03/serving-static-files-in-django/
I don't think you need your static path in urls.py, remove that and it should work.
currently it is like this
urlpatterns = patterns('',
(r'^$', index),
(r'^ajax/$', ajax),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'static')}),
)
just remove the r'^static line
urlpatterns = patterns('',
(r'^$', index),
(r'^ajax/$', ajax),
)
at least this is how it is done in django 1.3 and up
Try running python manage.py collectstatic
and see where the static files are being collected.
Add this to your urls.py
and set DEBUG=True
in settings.py
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes':True}),
)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
)