当前位置: 代码迷 >> python >> 未配置Django设置
  详细解决方案

未配置Django设置

热度:116   发布时间:2023-07-16 10:17:54.0

我刚刚卸载了python 3.6,并安装了python 3.7,然后在将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.).

然后当我尝试django-admin runserver时,它显示

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文档中:

使用Django时,必须告诉它所使用的设置。 通过使用环境变量DJANGO_SETTINGS_MODULE来执行此操作。

DJANGO_SETTINGS_MODULE的值应采用Python路径语法,例如mysite.settings 请注意,设置模块应位于Python导入搜索路径上。

示例(Windows Shell):

set DJANGO_SETTINGS_MODULE=mysite.settings
django-admin runserver

请参考以获取更多信息。

使用manage.py代替django-admin更容易,因为manage.py会为您设置DJANGO_SETTINGS_MODULE

使用django-admin启动项目:

django-admin startproject myproject

但是之后,请改用manage.py ,例如

python manage.py makemigrations
python manage.py migrate
python manage.py runserver