gpt4 book ai didi

python - DJANGO_SETTINGS_MODULE 未定义

转载 作者:行者123 更新时间:2023-11-28 22:25:05 25 4
gpt4 key购买 nike

我正尝试在 Django 中启动一个项目,但一开始就遇到了困难。每当我在没有命令的情况下运行 django-admin 时,我都会在帮助消息后收到此通知:

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.).

每当我尝试使用诸如 runserver 之类的命令运行它或 manage.py 时,我都会得到:

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.

我不知道如何定义这个 DJANGO_SETTINGS_MODULE,也不知道在哪里调用 settings.configure()。我已经尝试了这里建议的其他一些答案,但要么是不同的问题,要么我遗漏了一些应该很明显的东西。

最佳答案

manage.py 文件的顶部附近,您应该看到这一行,其中 project 是包含设置文件的项目的名称。

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

如果项目不存在,或者路径错误,python manage.py会失败。

文件树应该是这样的

.
├── manage.py
├── project
│   ├── settings.py

对于上下文,生成的 manage.py 包含以下行

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
# ... other code ...

关于python - DJANGO_SETTINGS_MODULE 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45805701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com