180

I'm using Django 1.6.5 with the setting:

DEBUG = True

When I change to DEBUG = False and run manage.py runserver, I get the following error:

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

I get the same error with the following setting:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

How can I fix this?

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Rancho
  • 1,988
  • 2
  • 12
  • 12
  • 2
    Check if below you defined again DEBUG = True. It could be just a bad copypaste. I got here because I canceled DEBUG = False by accident – Tms91 Apr 13 '20 at 16:36
  • From the posts below, there isn't even one solution which resolves this... – Conor Aug 11 '22 at 13:43
  • I was having same error while original code's version was different from installed django version, I accidently changed django version in requirement.txt & this issue comes up, by adding same version django in requirement txt and installing again fixed the issue – prdip Aug 04 '23 at 04:05

18 Answers18

242

Try

ALLOWED_HOSTS = ['*']

Less secure if you're not firewalled off or on a public LAN, but it's what I use and it works.

EDIT: Interestingly enough I've been needing to add this to a few of my 1.8 projects even when DEBUG = True. Very unsure why.

EDIT: This is due to a Django security update as mentioned in my comment.

Kye
  • 4,279
  • 3
  • 21
  • 49
  • Where should I put the code correctly, in order to not waste the code? – Малъ Скрылевъ Jul 30 '15 at 07:42
  • 5
    Do `grep ALLOWED_HOSTS . -ri` in your project's head folder to find the correct file. It's settings.py in this case, but grepping is always good advice. – Harald Nordgren Apr 14 '16 at 11:11
  • in which file should I write this? – Mona Jalal Jul 20 '16 at 21:21
  • @MonaJalal please see Harald's comment. – Kye Oct 14 '16 at 08:21
  • I am on django 1.8 and this works (@monajalal settings.py) – Bob Yoplait Nov 28 '16 at 17:24
  • 3
    According to [Django docs][1], the behavior of `ALLOWED_HOSTS` has been modified to address a DNS rebinding attack and this is probably necessary going forward. [1]: https://docs.djangoproject.com/en/1.10/ref/settings/#allowed-hosts – Kye Feb 24 '17 at 17:36
  • From django documentation: In older versions, ALLOWED_HOSTS wasn’t checked if DEBUG=True. This was also changed in Django 1.9.11 and 1.8.16 to prevent a DNS rebinding attack. – MouTio Mar 08 '17 at 09:54
  • It is worth commenting that Django does not even check ALLOWED_HOSTS when the host contains an underscore. More details in this question: https://stackoverflow.com/questions/6218390/underscore-in-subdomain-label-valid-or-not – Enric Calabuig May 14 '18 at 13:29
  • just what I was looking for. Django Version: 2.1.7, Python 3.6.7 working! – dclaudiud Feb 14 '19 at 13:00
  • NOT found The requested URL / was not found on this server. Hi Getting this reply – Hamza Tahir May 17 '19 at 10:33
  • @HamzaTahir this sounds like a separate issue. – Kye May 21 '19 at 03:07
78

Your solution might be to add the original IP and/or hostname also:

ALLOWED_HOSTS = [
  'localhost',
  '127.0.0.1',
  '111.222.333.444',
  'mywebsite.example']

The condition to be satisfied is that the host header (or X-Forwarded-Host if USE_X_FORWARDED_HOST is enabled) should match one of the values in ALLOWED_HOSTS.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
yeaske
  • 1,352
  • 14
  • 21
31

Make sure it's not redefined again lower down in your settings.py. The default settings has:

ALLOWED_HOSTS = []

Matt
  • 3,483
  • 4
  • 36
  • 46
17

From documentation: https://docs.djangoproject.com/en/1.10/ref/settings/

if DEBUG is False, you also need to properly set the ALLOWED_HOSTS setting. Failing to do so will result in all requests being returned as “Bad Request (400)”.

And from here: https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-ALLOWED_HOSTS

I am using something like this:

ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'www.mysite.com']
6

Use this:

ALLOWED_HOSTS =  ['localhost', '127.0.0.1']
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Fahadi Muhumuza
  • 131
  • 1
  • 5
2

If you work in PyCharm, check the Environmental variables for your Django server. You should specify the proper module.settings file

Daniel Chepenko
  • 2,229
  • 7
  • 30
  • 56
2

This works for me:

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
2

If you are using PyCharm

This solution applies only if you are using a different settings.py and have environment variables set

I had the same issue, but in my case the issue was, I was using a different settings.py file than the default (and had commented out my whole original settings.py), though I had it properly configured in my manage.py but in PyCharm I had to configure it as well in my Environment Variables via:

Edit Run Configurations >> Environment Variables

enter image description here

Aashish Gahlawat
  • 409
  • 1
  • 7
  • 25
1

In my case I had split out my settings.py into base.py and development.py in a settings folder. It looked something like:

<name-of-your-project-app>/
├── __init__.py
├── asgi.py
├── settings
│   ├── __init__.py
│   ├── base.py
│   └── development.py
├── urls.py
└── wsgi.py

The problem was much bigger than ALLOWED_HOSTS=... because none of the settings were recognized by python manage.py runserver.

The fix was to configure the DJANGO_SETTINGS_MODULE by running

export DJANGO_SETTINGS_MODULE=decoupled_dj.settings.development

in the command line. I think this tells Django, 'Hey look for my settings in the development.py'

zsega
  • 113
  • 7
0

Try

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header.

SHR
  • 7,940
  • 9
  • 38
  • 57
0

I had set ALLOW_HOSTS, INTERNAL_IPS and DEBUG=TRUE

but still got this error. my problem was i had created a python package which its name was 'settings' in main app. and that package name interfered with 'settings.py' file.

0

I also experienced the same error and found it is happening due to settings file configuration change. You have to configured few things as below mentioned.

Try

In settings.py

ALLOWED_HOSTS = ['*']

In manage.py

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your-project-name>.settings')

In asgi.py

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your-project-name>.settings')

In wsgi.py

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your-project-name>.settings')
0

So in my main directory in my django project, I had a two directories that had the same name so I deleted the settings folder I had and kept the settings.py file that comes with django.

What I had originally

<name-of-your-project-app>/
├── __init__.py
├── asgi.py
├── settings
│   ├── __init__.py
│   ├── base.py
│   └── development.py
├── settings.py
├── urls.py
└── wsgi.py

What I had afterwards

<name-of-your-project-app>/
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
Patrick Bateman
  • 121
  • 1
  • 2
0

Make sure the name of the directory is the same that you are mentioning in the INSTALLED_APPS. These are the solution to errors sometimes.

0

Faced the same error when using a .env file in development in a local setup on my computer which I didn't copy on the production server. As soon as I added the .env file on the production server the error was resolved. I have also added the server's IP Address to the allowed_hosts list on the settings file.

0

You are getting this error because settings.py does not recognized by Pycharm correctly. Therefore you must specify your settings.py file in Preferences.

Go to: Preferences -> Languages & Frameworks -> Django ->

  1. Enable Django Support
  2. Set Django project root to your main project folder
  3. Set Settings to your main_project/your_app/settings.py
  4. Set Manage script to your main_folder/manage.py

Pycharm Django Preferences

Hüseyin Aydın
  • 486
  • 5
  • 9
-1

I also experienced this cmderror. After trying all the answers on here, I couldn't still figure out the problem, here is what I did:

  1. Cd into the project directory. e.g cd project-dir
  2. I migrated. e.g python manage.py migrate
  3. I created a super user. e.g python manage.py createsuperuser
  4. Enter the desired info like username, password, email etc
  5. You should get a "super user created successfully" response
  6. Now run the server. E.g python manage.py runserver
  7. Click on the URL displayed
  8. The URL on your browser should look like this, 127.0.0.1:8000/Quit
  9. Now edit the URL on your browser to this, 127.0.0.1:8000/admin
  10. You should see an administrative login page
  11. Login with the super user info you created earlier on
  12. You should be logged in to the Django administration
  13. Now click on "view site" at the top of the page
  14. You should see a page which shows "the install worked successfully..... Debug = True"
  15. Voila! your server is up and running
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
-13

Just simply comment out the line: ALLOWED_HOSTS = [...]