Static django files not working

For some reason, django is not serving my static files.

I have already reviewed many fixes for this problem, but I still have not found a solution.

Here is my configuration:

urls.py

urlpatterns = patterns('', (r'^$', index), (r'^ajax/$', ajax), (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'static')}), ) 

settings.py

 STATIC_ROOT = '/home/aurora/Code/django/test/static/' STATIC_URL = '/static/' INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', ) 

When will I go to http://localhost:8000/static/css/default.css
I get this error: 'css/default.css' could not be found

When will I go to http://localhost:8000/static/
I get this error: Directory indexes are not allowed here.

It looks like the static directory is getting mapped, but the subdirectories are not.

+9
source share
4 answers

I don’t think you need your static path in urls.py, delete it and it should work.

currently it looks like

 urlpatterns = patterns('', (r'^$', index), (r'^ajax/$', ajax), (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'static')}), ) 

just delete the old line r '^

 urlpatterns = patterns('', (r'^$', index), (r'^ajax/$', ajax), ) 

at least the way it is done in django 1.3 and up

+5
source

In developing:

  • STATICFILES_DIRS must have all static directories inside which all static files are resident

  • STATIC_URL should be "/ static /" if your files are located on the local computer, otherwise specify the base URL here, for example. " http://example.com/ "

  • INSTALLED_APPS must include 'django.contrib.staticfiles'

In the template, load the staticfiles module:

 {% load staticfiles %} .. .. <img src='{% static "images/test.png" %}' alt='img' /> 

In production:

  • Add the "STATIC_ROOT" that django uses to collect all the static files from the "STATICFILES_DIRS" on it

  • Static File Collection

$ python manage.py collectstatic

  • add 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/

+11
source

Try running python manage.py collectstatic and see where the static files are going.

Add this to your urls.py and set DEBUG=True to 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}), ) 
+5
source
 STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, "static/") 
0
source

All Articles