升级到Django 1.7 - AppRegistryNotReady异常

3

我正在努力尝试将Django版本从1.6.7升级到1.7,但遇到了一些问题。看起来我没有抓住核心问题。现在我试着总结一下目前的情况。

问题是:如果我在wsgi.py文件中保留django.setup()命令,当我尝试访问我的网站时会出现一个内部服务器错误(500)。查看日志,我得到以下信息:

[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] mod_wsgi (pid=23258): Target WSGI script '/home/thrasher/webapps/django/myproject.wsgi' cannot be loaded as Python module.
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] mod_wsgi (pid=23258): Exception occurred processing WSGI script '/home/thrasher/webapps/django/myproject.wsgi'.
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1]   File "/home/thrasher/webapps/django/myproject.wsgi", line 16, in <module>
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1]     application = get_wsgi_application()
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1]   File "/home/thrasher/webapps/django/lib/python2.7/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1]     django.setup()
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1]   File "/home/thrasher/webapps/django/lib/python2.7/django/__init__.py", line 21, in setup
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1]     apps.populate(settings.INSTALLED_APPS)
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1]   File "/home/thrasher/webapps/django/lib/python2.7/django/apps/registry.py", line 78, in populate
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1]     raise RuntimeError("populate() isn't reentrant")
[Sun Oct 12 12:38:50 2014] [error] [client 127.0.0.1] RuntimeError: populate() isn't reentrant

然而,如果我注释掉django.setup()调用,试图访问网站会得到以下堆栈跟踪:
Environment:


Request Method: GET
Request URL: http://www.creepyvisions.it/

Django Version: 1.7
Python Version: 2.7.8
Installed Applications:
('django.contrib.auth',
 'django.contrib.admin.apps.SimpleAdminConfig',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'myproject.archivio',
 'sorl.thumbnail',
 'django.contrib.sitemaps',
 'rest_framework')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/thrasher/webapps/django/lib/python2.7/django/core/handlers/base.py" in get_response
  98.                 resolver_match = resolver.resolve(request.path_info)
File "/home/thrasher/webapps/django/lib/python2.7/django/core/urlresolvers.py" in resolve
  338.             for pattern in self.url_patterns:
File "/home/thrasher/webapps/django/lib/python2.7/django/core/urlresolvers.py" in url_patterns
  367.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/thrasher/webapps/django/lib/python2.7/django/core/urlresolvers.py" in urlconf_module
  361.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/home/thrasher/webapps/django/myproject/urls.py" in <module>
  33.                        url(r'^admin/', include(admin.site.urls)),
File "/home/thrasher/webapps/django/lib/python2.7/django/contrib/admin/sites.py" in urls
  260.         return self.get_urls(), self.app_name, self.name
File "/home/thrasher/webapps/django/lib/python2.7/django/contrib/admin/sites.py" in get_urls
  221.             self.check_dependencies()
File "/home/thrasher/webapps/django/lib/python2.7/django/contrib/admin/sites.py" in check_dependencies
  159.         if not apps.is_installed('django.contrib.admin'):
File "/home/thrasher/webapps/django/lib/python2.7/django/apps/registry.py" in is_installed
  223.         self.check_apps_ready()
File "/home/thrasher/webapps/django/lib/python2.7/django/apps/registry.py" in check_apps_ready
  124.             raise AppRegistryNotReady("Apps aren't loaded yet.")

Exception Type: AppRegistryNotReady at /
Exception Value: Apps aren't loaded yet.

为了完整性,这是与wsgi相关的代码:

myprojext.wsgi

import os
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

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

wgsi.py

import django
from django.core.handlers.wsgi import WSGIHandler


def get_wsgi_application():
    #django.setup()
    return WSGIHandler()

我觉得情况很奇怪,我在官方Django文档和各种论坛上进行了大量搜索,但仍然无法使事情正常工作。如果有任何建议,将不胜感激。


你的代码中是否使用了 get_user_model() 函数?你是否在代码中其他地方调用了 django.setup() 函数? - Kevin Christopher Henry
嗨@KevinChristopherHenry,不,我没有使用那些方法。问题是在INSTALLED_APPS中重复了django.contrib.admin。看起来这是问题的根源。当我删除第二个引用并取消注释wsgi.py中的django.setup()时,一切都变得明朗和清晰。现在一切正常运作。 - Luca Trifilio
@LucaTrifilio 请将此作为答案编写并接受。否则,此问题将继续显示为未回答的问题。 - boatcoder
1个回答

2

问题是 INSTALLED_APPS 中 django.contrib.admin 重复了,这似乎是问题的根源。一旦我移除了第二个引用,在 wsgi.py 中取消注释 django.setup(),一切又变得清晰明朗了。现在一切都正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接