django报错:ImproperlyConfigured和AppRegistryNotReady

时间:2021-05-21 08:50:33

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。。。。。。