Django cannot find staticfiles with Debug = False and Allowed_Hosts

Hi everyone, I am having problems resolving this problem: if I return to DEBUG in False, I cannot run manage.py runningerver:

CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False 

Then let's say I add something to ALLOWED_HOSTS:

 ALLOWED_HOSTS = ['*'] or ALLOWED_HOSTS = ['localhost'] or ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] 

Now I can do'manage.py runningerver ', but static files do not work. Weird

If I believe DEBUG in True, then it will work with ALLOWED_HOSTS set by nothing, on localhost or *. So, I think the problem is with DEBUG. I do not understand this.

+6
source share
2 answers

In DEBUG mode, the Django development server processes static files for you. However, this is not suitable for production, as it is much more inefficient than a real server. See here .

File service

In addition to these configuration steps, you will also need to execute static files.

During development, if you use django.contrib.staticfiles, this will be done automatically by the launch server when DEBUG is set to True (see django.contrib.staticfiles.views.serve ()).

This method is extremely inefficient and probably unsafe, therefore it is unsuitable for production.

See Deploying Static Files for the Right Strategies for Serving Static Files in Production Environments.

Check here to learn how to handle static files during production.

EDIT: Add the following to answer @alejoss's question about viewing error pages with DEBUG = True.

I added something like the following to my urls.py root file:

 if settings.DEBUG: urlpatterns += patterns( '', url(r'^400/$', TemplateView.as_view(template_name='400.html')), url(r'^403/$', TemplateView.as_view(template_name='403.html')), url(r'^404/$', 'django.views.defaults.page_not_found'), url(r'^500/$', 'django.views.defaults.server_error'), ) 

You may need to change a bit (for example, pages 400 and 403 , you may need to edit if the names of your templates are different). Basically, this allows you to visit http://localhost/400 to see a page with 400 error, http://localhost/403 to see a page with 403 error, etc.

+10
source

If you still need to locally statically on the server (for example, for testing without debugging), you can run devserver in unsafe mode:

 manage.py runserver --insecure 
+3
source

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


All Articles