1.报错信息:
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG,but settings are not configured. You must either define the environmentvariable DJANGO_SETTINGS_MODULE or call settings.configure() before accessingsettings.
需要添加两行语句:
import os
os.environ['DJANGO_SETTINGS_MODULE']= 'mysite.settings'
注释:在什么情况下出现上述错误呢?在我编写程序的过程遇到这个错误时,是当我引入models时,比如在文件开始我引包时from database.models import list_url_save,就会出现这个提示的错误。是因为程序没有识别出database这个应用,我已经把database应用添加到settings文件INSTALLED_APPS中了,但还是需要在文件开头添加那两行环境变量的配置语句,让程序知道去哪里寻找models中的文件。
2.报错信息:
"The translation infrastructure cannotbe initialized before the "
django.core.exceptions.AppRegistryNotReady: The translation infrastructurecannot be initialized before the apps registry is ready. Check that you don'tmake non-lazy gettext calls at import time.
需要添加两行语句:
import django
django.setup()
两者的引用顺序也是有要求的,先引用import os 再引用import django。。。。。。
本文介绍了两种常见的Django启动配置问题及其解决方案。一是未正确配置环境变量导致的models无法识别问题;二是翻译基础设施初始化前应用注册未就绪的问题。通过添加特定的环境变量配置和调用setup方法可以有效解决。
2608





