[Fixed]-Django isn't serving static files

0👍

The answer was very subtle. When I upgraded Django to 1.9 and ran the server, it gave the following warning:

?: (urls.W001) Your URL pattern '^static/(?P<path>.*)$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.

In urls.py, my urlpatterns list contained:

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

I changed it to:

url(r'^static/(?P<path>.*)/', 'django.views.static.serve', {
    'document_root': settings.STATIC_ROOT,
}),

This eliminated the warning but caused static resources to stop loading.
It needed to be:

url(r'^static/(?P<path>.*)', 'django.views.static.serve', {
    'document_root': settings.STATIC_ROOT,
}),

It’s still a mystery to me why this worked on my dev machine (a Macbook), as well as another on the team’s dev machine (a windows laptop), but not on our Linux server. But, it works now, so I’m done trying to figure it out.

1👍

Django does not serve static files in production mode (DEBUG=False). On a production deployment that’s the job of the webserver. To resolve the problem:

  • run python manage.py collectstatic
  • in your web server configuration point the /static folder to the static folder of Django

Don’t just turn DEBUG on, it would be dangerous!

0👍

Try to change os.path.join(PROJECT_DIR, ‘../static’) to os.path.join(PROJECT_DIR, ‘static’) and STATIC_ROOT = os.path.join(PROJECT_DIR, ‘../static_resources’) to STATIC_ROOT = os.path.join(PROJECT_DIR, ‘static_resources’). It will solve your problem.

Leave a comment