首页 > 解决方案 > 尝试安装 django-seo 时出现 AppRegistryNotReady 错误

问题描述

我跑了pip install djangoseo,然后用 pip freeze 确认了

添加rollyourown.seo到我的INSTALLED_APPS设置

添加"django.core.context_processors.request"TEMPLATE_CONTEXT_PROCESSORS设置

当我尝试运行 pythonmanage.py syncdb或运行我的应用程序时,出现以下错误

AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

标签: pythondjango

解决方案


当您尝试在 django 加载模型之前访问模型时,会引发 AppRegistryNotReady("Apps are not loaded yet.") 。在apps.py 文件中,覆盖ready 方法,它是一个触发器,说明django 已经完成了它的工作,模型的导入,或者任何与模型相关的东西都应该在那里。

functions.py

def whatever():
    from . import models


__init__.py

from .functions import whatever

“whatever”函数可能在内部导入模型。因此,在应用程序开始时,您尝试导入导入模型的任何内容,而 django 尚未完成加载它,因此出现异常。

解决方案:

__init__.py

default_app_config = 'users.apps.UserConfig'


apps.py

class UserConfig(AppConfig):
    name = 'users'

    def ready(self):
        try:
            from .functions import whatever

        except Exception as e:
            print("DON'T WORRY")

推荐阅读