No static files when DEBUG False

I had a problem deploying my application on a production server. If I set in settings.py

DEBUG = True TEMPLATE_DEBUG = DEBUG 

then everything works fine, but if I changed the settings to:

 DEBUG = False TEMPLATE_DEBUG = DEBUG 

then my application is broken. I do not see static files (js, css, ...), and in the admin panel I can not add / edit my registered models. Do you have any tips how can I solve this problem?

My envoirment:

  • Python 2.7.3
  • Django 1.4.1
  • Nginx 1.2.3
  • uwsgi 1.3
+4
source share
2 answers

You should read: Serving static files during production.

At least it looks like you are relying on the helper view staticfiles_urlpatterns() from contrib.staticfiles, which is for development purposes only:

This will only work if DEBUG is True.

This is because this opinion is extremely ineffective and probably unsafe. It is intended for local development only and should never be used in production.

+10
source

I also had this problem, I solved it.

Just try

in settings.py

 DEBUG = False 

then add

 DEBUG404 = True ALLOWED_HOSTS = ['*'] # it works but not secure, so use ALLOWED_HOSTS = ['localhost', 'IP adrs'] #if you are running locally, then run with python manage.py runserver --insecure.You can give your webserver here. 

Then in urls.py add

 import os if settings.DEBUG404: urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': os.path.join(os.path.dirname(__file__), 'static')} ), ) 
+1
source

Source: https://habr.com/ru/post/1438304/


All Articles