我正在尝试在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()。我已经尝试过这里提出的其他一些答案,但要么是一个不同的问题,要么我错过了一些非常明显的答案。
答案 0 :(得分:4)
在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 ...