I am new to django-1.6. When I run the django server with DEBUG = True, it’s running perfectly. But when I change DEBUG to False in the settings file, then the server stopped and it gives the following error on the command prompt:
CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
After I changed ALLOWED_HOSTS to ["http://127.0.0.1:8000",], in the browser I get the error:
The ALLOWED_HOSTS list should contain fully qualified host names, not urls. Leave out the port and the protocol. If you are using 127.0.0.1, I would add localhost to the list too:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
You could also use * to match any host:
ALLOWED_HOSTS = ['*']
Quoting the documentation:
Values in this list can be fully qualified names (e.g. 'www.example.com'), in which case they will be matched against the request’s Host header exactly (case-insensitive, not including port). A value beginning with a period can be used as a subdomain wildcard: '.example.com' will match example.com, www.example.com, and any other subdomain of example.com. A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE_CLASSES).
Bold emphasis mine.
The status 400 response you get is due to a SuspiciousOperation exception being raised when your host header doesn’t match any values in that list.
I had the same problem and I fixed it by setting ALLOWED_HOSTS = ['*'] and to solve the problem with the static images you have to change the virtual paths in the environment configuration like this:
I had the same problem and none of the answers resolved my problem, for resolving the situation like this it’s better to enable logging by adding the following config to settings.py temporary
and try to tail -f /tmp/debug.log.
and when you see your issue you can handle it much easier than blind debugging.
My issue was about to
Invalid HTTP_HOST header: ‘pt_web:8000’. The domain name provided is not valid according to RFC 1034/1035.
and resolve it by adding proxy_set_header Host $host; to Nginx config file and enabling port forwarding by USE_X_FORWARDED_PORT = True in the settings.py ( it’s because in my case I’ve listened to request in Nginx on port 8080 and pass it to guni on port 8000
so I change the url to 127.0.1.1:(the used port)/project
and voila !
you have to check what is your virtual network address, for me as i use bitnami django stack 2.2.3-1 on Linux i can check which port django is using.
if you have an error ( 400 bad request ) then i guess django on different virtual network ..
good luck
With DEBUG = False in you settings file, you also need ALLOWED_HOST list set up.
Try including ALLOWED_HOST = ['127.0.0.1', 'localhost', 'www.yourdomain.com']
Otherwise you might receive a Bad Request(400) error from django.