2

I am using django 3.2.8. I set DEBUG = True and ALLOWED_HOSTS = ['*'] in my project settings.py file. When I try to "python3 manage.py runserver" it gives below error.

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

It is strange, because DEBUG is set to True in settings.py. But it says debug is false. Also I have this setting in my other project and it is working with Debug=True.

ivbtar
  • 799
  • 11
  • 29
  • 1
    i made python manage.py diffsettings --all. It shows all settings. Output ise Debug = False and ALLOWED_HOSTS=[]. But when i check my project directory settigns.py it is DEBUG = True and ALLOWED_HOSTS = ['*']. It seems my overwrite on settings.py does not effect. It seems django is reading settings.py from somewhere else. – ivbtar Oct 17 '21 at 12:46
  • Can you include your project structure and what the `DJANGO_SETTINGS_MODULE` environment variable is set to or is defaulting to in `manage.py` – Iain Shelvington Oct 17 '21 at 12:51
  • You are right lain. Django cant find settings. I check manage.py, it is os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'iomobproj.settings') line exists. Also when i printenv DJANGO_SETTINGS_MODULE=iomobproj.settings. But still it is looking into global django settings file. – ivbtar Oct 17 '21 at 13:04
  • Problem was, there was an error in settings.py file. Django was not able to use settings.py file so it was trying to use global settings.py file. Now problem is solved as i corrected settings.py file. – ivbtar Oct 17 '21 at 13:36
  • Does this answer your question? [CommandError: You must set settings.ALLOWED\_HOSTS if DEBUG is False](https://stackoverflow.com/questions/24857158/commanderror-you-must-set-settings-allowed-hosts-if-debug-is-false) – Ruby Oct 17 '21 at 16:07
  • This behaviour happens when there is a problem in settings file. So django reads global settings file and does not see your settings file. Debug=True in my settings, but Django was showing that Debug=False. Because it was not looking into project settings file as there was an error in it. Error was about importing a library. I deleted the related import lines and now error is gone. – ivbtar Oct 18 '21 at 11:26

1 Answers1

6

This behaviour happens when there is a problem in settings file. So django reads global settings file and does not see your settings file. Debug=True in my settings, but Django was showing that Debug=False. Because it was not looking into project settings file as there was an error in it. Error was about importing a library in settings.py. I deleted the related import lines and now error is gone. So if the "python manage.py diffsettings --all" command shows wrong django settings, it means your project settings file has a problem and django is trying to get settings from global django settings file which is not specialized for your project.

ivbtar
  • 799
  • 11
  • 29