Django 1.7升级错误:AppRegistryNotReady:应用尚未加载


68

我正在尝试将项目从Django 1.6升级到1.7。我收到以下错误:

[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] mod_wsgi (pid=14523): Exception occurred processing WSGI script '/home/users1/k/kisvadim/domains/mathtasks.org/django.wsgi'.
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] Traceback (most recent call last): 
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] response = self.get_response(request)
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/base.py", line 199, in get_response
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/base.py", line 239, in handle_uncaught_exception
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] if resolver.urlconf_module is None:
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 361, in urlconf_module
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] self._urlconf_module = import_module(self.urlconf_name)
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] __import__(name)
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/mathtasks/urls.py", line 11, in <module>
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] admin.autodiscover()
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] autodiscover_modules('admin', register_to=site)
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/utils/module_loading.py", line 67, in autodiscover_modules
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] for app_config in apps.get_app_configs():
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] self.check_apps_ready()
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] raise AppRegistryNotReady("Apps aren't loaded yet.")
[Thu Oct 09 14:16:41 2014] [error] [client 95.79.172.156] AppRegistryNotReady: Apps aren't loaded yet.

django.wsgi:

import os, sys
import django
virtual_env = os.path.expanduser('~/virtualenv/PythonEnv')
activate_this = os.path.join(virtual_env, 'bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
sys.path.insert(0, os.path.join(os.path.expanduser('~'), 'django/mathtasks'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'mathtasks.settings'
django.setup()
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

任何想法可能导致错误的原因以及如何解决?

UPDATE1:

我更换:

from django.contrib.auth.models import User

至:

from django.conf import settings
User = settings.AUTH_USER_MODEL

我收到以下错误:

[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] mod_wsgi (pid=17128): Exception occurred processing WSGI script '/home/users1/k/kisvadim/domains/mathtasks.org/django.wsgi'. 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] Traceback (most recent call last): 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__ 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] response = self.get_response(request) 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/base.py", line 199, in get_response 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] response = self.handle_uncaught_exception(request, resolver, sys.exc_info()) 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/base.py", line 239, in handle_uncaught_exception 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] if resolver.urlconf_module is None: 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 361, in urlconf_module 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] self._urlconf_module = import_module(self.urlconf_name) 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] __import__(name) 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/mathtasks/urls.py", line 6, in <module> 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] from utils.views import SniptRegistrationView 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/utils/views.py", line 1, in <module> 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] from registration.backends.default.views import RegistrationView 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/registration/backends/default/views.py", line 7, in <module> 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] from registration.views import ActivationView as BaseActivationView 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/registration/views.py", line 11, in <module> 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] from registration.forms import RegistrationForm 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/registration/forms.py", line 159, in <module> 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] class UserProfileForm(forms.ModelForm): 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/forms/models.py", line 284, in __new__ 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] opts.help_texts, opts.error_messages) 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/forms/models.py", line 184, in fields_for_model 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] opts = model._meta 
[Thu Oct 09 15:06:15 2014] [error] [client 95.79.172.156] AttributeError: 'str' object has no attribute '_meta'

更新2:

我更换:

from django.contrib.auth.models import User

至:

from django.contrib.auth import get_user_model
User = get_user_model()

和:

models.ForeignKey(settings.AUTH_USER_MODEL)

我收到以下错误:

[Thu Oct 09 16:10:52 2014] [error] No handlers could be found for logger "django.request" 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] mod_wsgi (pid=31421): Exception occurred processing WSGI script '/home/users1/k/kisvadim/domains/mathtasks.org/django.wsgi'. 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] Traceback (most recent call last): 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__ 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] response = self.get_response(request) 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/base.py", line 199, in get_response 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] response = self.handle_uncaught_exception(request, resolver, sys.exc_info()) 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/handlers/base.py", line 239, in handle_uncaught_exception 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] if resolver.urlconf_module is None: 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 361, in urlconf_module 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] self._urlconf_module = import_module(self.urlconf_name) 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] __import__(name) 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/mathtasks/urls.py", line 6, in <module> 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] from utils.views import SniptRegistrationView 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/utils/views.py", line 1, in <module> 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] from registration.backends.default.views import RegistrationView 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/registration/backends/default/views.py", line 6, in <module> 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] from registration.models import RegistrationProfile 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/django/mathtasks/registration/models.py", line 9, in <module> 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] User = get_user_model() 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 136, in get_user_model 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] return django_apps.get_model(settings.AUTH_USER_MODEL) 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/apps/registry.py", line 199, in get_model 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] self.check_models_ready() 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] File "/home/users1/k/kisvadim/virtualenv/PythonEnv/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] raise AppRegistryNotReady("Models aren't loaded yet.") 
[Thu Oct 09 16:10:52 2014] [error] [client 95.79.172.156] AppRegistryNotReady: Models aren't loaded yet.

Answers:



73

升级到Django 1.7时,我遇到了类似的问题(相同?)。在可能的情况下,只需更新wsgi文件即可:替换

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

2
花了我几个小时才偶然发现。我也得到了{无法找到记录程序“ django.request”的处理程序},这立即解决了我的问题。谢谢!!
Logic1 2015年

我只有在生产时才有这种花。现在就像魅力一样!
benzkji

谢谢!解决了我只能通过以下方式运行服务器的问题:./manage.py runserver,而不是在Django 1.5升级到1.11之后通过gunicorn运行
SwCharlie

20

我一直在浏览所有这些线程,以使芹菜在Windows上工作(带有此特定错误消息),我只想强调

import django
django.setup()

在您task.py文件的顶部,这终于使它对我有用。


12

INSTALLED_APPS 数组中缺少逗号,这对我造成了错误。


10

当我尝试从应用程序导入模型时出现此错误__init__.py,这是不可能的。


不只是模型,因为我试图导入url并遇到了同样的问题
Abhishek Jebaraj

您在init声明模型吗?很奇怪...您应该在模型中声明...,也许,如果必须先加载软件包,则可以在init加载一些模型。
DiegoVinícius18年

2
我不建议这样做,而是说它会导致错误
Mark

6

在配置uwsgi应用程序文件(如uwsgi.ini)中,替换为:

module = django.core.handlers.wsgi:WSGIHandler()

module=django.core.wsgi:get_wsgi_application()

1

settings.AUTH_USER_MODEL是一个字符串(这就是为什么要得到AttributeError: 'str' object has no attribute '_meta'它-它需要一个Model),并且只能在ForeignKey声明中使用:

类Article(models.Model):
    作者= models.ForeignKey(settings.AUTH_USER_MODEL)

如果要在其他地方引用用户模型,则需要使用get_user_model

从django.contrib.auth导入get_user_model

UserModel = get_user_model()

1

当移动到另一台PC并忘记psycopg2在venv中安装和使用时,出现此错误'ENGINE': 'django.db.backends.postgresql',在设置中。

也许对其他人有用。


1

就我而言,SEcret_key在settings.py中被错误地注释了


0

就我而言,我忘记在其中添加子应用程序之一INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'basketball',
    'basketball.area',
    'basketball.game',
    'basketball.player',
]

0

就我而言,该错误是我将celery升级到4.x时引起的,而我的INSTALLED_APPS中包含此错误:kombu.transport.django。删除该问题已解决。似乎主要是由于django本身的一些不正确加载引起的。


0

对于那些(例如我)从shell中得到此错误的人:

检查您正在执行的是Django的shell,而不是Python的shell。

python manage.py shell


-3

可能是任何原因。就我而言,我在虚拟环境之外更新了python版本,但是我的virtualenv仍然是旧版本,这导致了错误。我删除了env并使用较新的版本再次构建它,并且可以正常工作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.