I'm trying to start a project in Django, but I've come up against a wall right at the start. Whenever I run django-admin with no commands, I get this notice after the help message:
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
And whenever I try to run it or manage.py with a command such as runserver, I get:
python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 62, in execute
super(Command, self).execute(*args, **options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 73, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/conf/__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I don't know how to define this DJANGO_SETTINGS_MODULE nor where to call settings.configure(). I've tried what some other answers here suggested, but either it's a different problem, or I'm missing something that should be quite obvious.
Near the top of your manage.py
file, you should see this line, where project is the name of your project which contains the settings file.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
If the project does not exist, or the path is wrong, python manage.py
will fail.
The file tree should look something like this
.
├── manage.py
├── project
│ ├── settings.py
For context, a generated manage.py
contains the following lines
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
# ... other code ...
You may use the suggested solution of an env variable in the exception - DJANGO_SETTINGS_MODULE.
export DJANGO_SETTINGS_MODULE="project.settings"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With