Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django can't find staticfiles with Debug=False and Allowed_Hosts

Hi all I'm having trouble solving this issue: If I turn DEBUG to False, I can't run manage.py runserver:

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 runserver´ but the staticfiles don't work. Weird.

If I turn DEBUG to True, then it works with ALLOWED_HOSTS set to nothing, to localhost or to *. So, I guess the problem has to do with DEBUG. I don't understand it.

like image 764
Alejandro Veintimilla Avatar asked Feb 10 '15 23:02

Alejandro Veintimilla


People also ask

Why does DEBUG false setting make my Django static files access fail?

Learning programming: Why does DEBUG=False setting make my Django Static Files Access fail? Because the Django development server does not serve static files. You need to have a web server like nginx or Apache to serve your static files.

How do I change DEBUG to false?

To disable debug mode, set DEBUG = False in your Django settings file.

What does DEBUG false do Django?

Open your settings.py file (or settings_local.py ) and set DEBUG = False (just add that line if necessary). Turning off the Django debug mode will: Suppress the verbose Django error messages in favor of a standard 404 or 500 error page. You will now find Django error messages printed in your arches.


3 Answers

If you still need to server static locally (e.g. for testing without debug) you can run devserver in insecure mode:

manage.py runserver --insecure
like image 134
Harryface Avatar answered Oct 13 '22 20:10

Harryface


Okay Here's the very clean solution. you need to use

DEBUG = False
DEBUG_PROPAGATE_EXCEPTIONS = True

This way in your logs you can see what is then problem. Whitenoise returns 500 usually when It is missing some file.

You can see what is missing in you logs. In my case heroku logs were enough.

for more info: https://docs.djangoproject.com/en/2.0/ref/settings/#debug-propagate-exceptions

like image 27
Ojas Kale Avatar answered Oct 13 '22 20:10

Ojas Kale


In DEBUG mode, the Django development server handles serving static files for you. However, this is not best for production as it's much more inefficient than a true server. See here.

Serving the files

In addition to these configuration steps, you’ll also need to actually serve the static files.

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

This method is grossly inefficient and probably insecure, so it is unsuitable for production.

See Deploying static files for proper strategies to serve static files in production environments.

Check out here to learn out how to serve static files in production.

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

I added something like the following to my root urls.py 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 might need to alter a bit (i.e., the 400 and 403 pages may need to be edited if your template names are different). Basically, this lets you visit http://localhost/400 to see your 400 error page, http://localhost/403 to see your 403 error page, and so on.

like image 33
Alex Avatar answered Oct 13 '22 21:10

Alex