-
-
Notifications
You must be signed in to change notification settings - Fork 145
Description
Hi there 👋
TL;DR
When an exception occurs during the loading of the settings, it's written into settings_exception of the ManagementUtility, but never logged on stdout.
Context
I'm jumping in an existing repo where django-configurations is already (but partially to my needs) installed. Trying to refactor things lightly, I used the DatabaseURLValue to directly read from a URL instead. You may see this coming, but it failed silently with no information. The exception was swallowed, and only a vague error message was printed afterwards. Digging into things...
❯ uv run manage.py runserver
> /Users/valz/Documents/work/app/.venv/lib/python3.12/site-packages/django/core/management/__init__.py(443)execute_from_command_line()
441 utility = ManagementUtility(argv)
442 breakpoint()
--> 443 utility.execute()
ipdb> utility
<django.core.management.ManagementUtility object at 0x102e3e780>
ipdb> vars(utility)
{'argv': ['manage.py', 'runserver'], 'prog_name': 'manage.py', 'settings_exception': None}
ipdb> r
CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
--Return--
None
> /Users/valz/Documents/work/app/.venv/lib/python3.12/site-packages/django/core/management/__init__.py(443)execute_from_command_line()
441 utility = ManagementUtility(argv)
442 breakpoint()
--> 443 utility.execute()
ipdb> vars(utility)
{'argv': ['manage.py', 'runserver'], 'prog_name': 'manage.py', 'settings_exception': ImproperlyConfigured("Could not import 'dj_database_url.parse'")}
ipdb>Once ManagementUtility.execute() runs, we can see I received a generic
"CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
It's true, because I suspect it fell back to some basic default values (django.conf.settings is barely filled in), which gave me the clue something broke internally, and indeed, by checking the internal vars of the ManagementUtility, it shows the real answer and its fix (uv add "django-configurations[database]")
'settings_exception': ImproperlyConfigured("Could not import 'dj_database_url.parse'")
Expected
If I use things coming from extra that aren't installed, the exception is logged. E.g
❯ uv run manage.py runserver
CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
Hint: An internal exception occurred: "Could not import 'dj_database_url.parse'". Did you installed the required extras?
Should this issue make sense, and I didn't goofed myself by not activating a flag that could've showed me immediately why I was being stupid, I'd be happy to help writing a quick PR fixing this :)
Have a nice one