300

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:

Bad Request (400)

Is it possible to run Django without debug mode?

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
codeimplementer
  • 3,303
  • 2
  • 14
  • 16

11 Answers11

488

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
15

I had the same problem and none of the answers resolved my problem. For resolving situations like this, it's best to enable logging by adding the following config to settings.py temporarily.

LOGGING = {
   'version': 1,
   'disable_existing_loggers': False,
   'handlers': {
      'file': {
         'level': 'DEBUG',
         'class': 'logging.FileHandler',
         'filename': '/tmp/debug.log',
      },
   },
   'loggers': {
      'django': {
         'handlers': ['file'],
         'level': 'DEBUG',
         'propagate': True,
      },
   },
}

When you see the issue, it's easier to handle than by blind debugging.

My issue was:

Invalid HTTP_HOST header: 'pt_web:8000'. The domain name provided is not valid according to RFC 1034/1035.

I resolved it by adding proxy_set_header Host $host; to the Nginx config file and enabling port forwarding with USE_X_FORWARDED_PORT = True in the settings.py (it's because in my case I was listening to requests on Nginx port 8080 and passing to guni on port 8000).

Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
Yuseferi
  • 7,931
  • 11
  • 67
  • 103
  • Thanks for sharing. For my case in prod, after I add the LOGGING=..., then I see the error "ValueError: Missing staticfiles manifest entry for...css". Then I use "python manager.py collectstatic" as mentioned above by @Blackeagle52, the 500 error (can also be 400 error in my local dev) is solved. – zhihong Oct 03 '18 at 20:25
  • I'm getting `ValueError: Unable to configure handler 'file': [Errno 2] No such file or directory: '/var/log/app-log/django.log'. ` – fIwJlxSzApHEZIl Feb 11 '22 at 16:39
  • I changed the path to `django.log` to try to get around the missing file error and there's a log file created now but it's empty. – fIwJlxSzApHEZIl Feb 11 '22 at 17:13
  • I had the exact same "RFC 1034/1035" error, but it was because of an underscore in my url. Check this before trying to modify nginx or Django confs. – David Dahan Jul 04 '23 at 13:52
7

For me, I got this error by not setting USE_X_FORWARDED_HOST to true. From the docs:

This should only be enabled if a proxy which sets this header is in use.

My hosting service wrote explicitly in their documentation that this setting must be used, and I get this 400 error if I forget it.

ki9
  • 5,183
  • 5
  • 37
  • 48
4

in the settings.py of your project, check line 28, where is the Allows Host

settings.py


ALLOWED_HOSTS = ['IP', 'servidor', ]

you must put the IP and the server you use, level local or web settings.py

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.ejemplo.com']

or

ALLOWED_HOSTS = ['*']
3

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:

Virtual Path                 Directory

/static/                          /opt/python/current/app/yourpj/static/
/media/                        /opt/python/current/app/Nuevo/media/

I hope it helps you.

PD: sorry for my bad english.

Salem
  • 13,516
  • 4
  • 51
  • 70
Jorge
  • 31
  • 1
2

For me as I have already xampp on 127.0.0.1 and django on 127.0.1.1 and i kept trying adding hosts

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.yourdomain.com', '*', '127.0.1.1']

and i got the same error or (400) bad request enter image description here

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 enter image description here

SIGSTACKFAULT
  • 919
  • 1
  • 12
  • 31
1

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.

Abhishek Lodha
  • 737
  • 2
  • 7
  • 30
0

I had to stop the apache server first.

(f.e. sudo systemctl stop httpd.service / sudo systemctl disable httpd.service).

That solved my problem besides editing the 'settings.py' file

to ALLOWED_HOSTS = ['se.rv.er.ip', 'www.example.com']

Ingo Mi
  • 999
  • 12
  • 26
0

try manage.py collectstatic. I was missing a static file after an update, hence the bad request.

run_the_race
  • 1,344
  • 2
  • 36
  • 62
0

There are two other reasons I know (less frequent but still) that can cause 400 bad request:

  1. Nginx Doesn’t Pass $Host To Your Application
  2. Host Name Has Underscores

I outlined these examples here

Andrii Zarubin
  • 2,165
  • 1
  • 18
  • 31
-3

Try to run your server with the --insecure flag, just like this:

python manage.py runserver --insecure
Paolo
  • 20,112
  • 21
  • 72
  • 113
Alberto
  • 1
  • 1