Static files won't load when out of debug in Django

梦想与她 提交于 2019-11-28 18:23:27

Static files app is not serving static media automatically in DEBUG=False mode. From django.contrib.staticfiles.urls:

# Only append if urlpatterns are empty
if settings.DEBUG and not urlpatterns:
    urlpatterns += staticfiles_urlpatterns()

You can serve it by appending to your urlpatterns manually or use a server to serve static files (like it is supposed to when running Django projects in non-DEBUG mode).

Though one thing I am wondering is why you get a 500 status code response instead of 404. What is the exception in this case?

EDIT

So if you still want to serve static files via the staticfiles app add the following to your root url conf (urls.py):

if settings.DEBUG is False:   #if DEBUG is True it will be served automatically
    urlpatterns += patterns('',
            url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

Some things you need to keep in mind though:

  • don't use this on a production environment (its slower since static files rendering goes through Django instead served by your web server directly)
  • most likely you have to use management commands to collect static files into your STATIC_ROOT (manage.py collectstatic). See the staticfiles app docs for more information. This is simply necessary since you run on non-Debug mode.
  • don't forget from django.conf import settings in your urls.py :)

In Django 1.3, if you are just testing using the manage.py runserver, you can add the option "--insecure", as described in the staticfiles docs:

It seems to still send emails to the admin saying that there is no template, but it does serve static files.

I'm not sure if the email issue is on purpose or a bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!