2016-01-08 45 views
2

我从Django的1.6升级到1.7,当我尝试做manage.py runserver我得到以下跟踪:升级到Django的1.7:获得AppRegistryNotReady翻译基础设施

Traceback (most recent call last): 
    File "manage.py", line 9, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line 
    utility.execute() 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute 
    django.setup() 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup 
    apps.populate(settings.INSTALLED_APPS) 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate 
    app_config = AppConfig.create(entry) 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/apps/config.py", line 87, in create 
    module = import_module(entry) 
    File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/home/ben/Code/Repos/myrepo/myproject/core/mail/__init__.py", line 6, in <module> 
    from myproject.core.mail.models import IMapEmailMessage, EmailStatus 
    File "/home/ben/Code/Repos/myrepo/myproject/core/mail/models.py", line 20, in <module> 
    from myproject.core.mail.utils import render_templates 
    File "/home/ben/Code/Repos/myrepo/myproject/core/mail/utils.py", line 19, in <module> 
    from myproject.core.util import clean_html 
    File "/home/ben/Code/Repos/myrepo/myproject/core/util.py", line 1031, in <module> 
    def make_url(url, text=_('here')): 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 83, in ugettext 
    return _trans.ugettext(message) 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 325, in ugettext 
    return do_translate(message, 'ugettext') 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 306, in do_translate 
    _default = translation(settings.LANGUAGE_CODE) 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 209, in translation 
    default_translation = _fetch(settings.LANGUAGE_CODE) 
    File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 189, in _fetch 
    "The translation infrastructure cannot be initialized before the " 
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time. 

我没有使用过的应用程序的注册表之前,所以我假定在使用翻译之前,需要在我的应用程序中完成一些设置。我不断看到解决的办法是把它添加到wsgi.py

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

不过我已经有这些线路在那里。在文件myproject/core/util.py我改变了以下行:

from django.utils.translation import ugettext as _ 

到:

from django.utils.translation import ugettext_lazy as _ 

而这只是移动的问题,其使用的ugettext另一个文件。是否不再可以使用非懒惰ugettext?还是有一些设置我需要做,以避免它在进口时被评估?

+0

这不是一个答案!为什么你不想使用最新版本? 1.9 –

+2

我将逐步升级到1.8,但首先尝试1.7。 – benwad

+1

我认为跟踪的最后一行非常重要,“检查你是否在导入时不进行非惰性gettext调用。”,所以是的,在导入时需要懒惰版本 – Sayse

回答

2

使用make_url(url, text=ugettext('here'))的问题是,对于text默认参数计算当模块被导入,而不是当该make_url函数运行。

您没有显示生成第二个错误的代码,所以我不知道它出了什么问题。

在函数内部使用ugettext(只要该函数在导入期间不运行)就可以了。例如:

def make_url(url, text=None): 
    if text is None: 
     text = ugettext('here') 

注意,你仍然可以在你的代码做import uggettext as _,我只是用ugettext以上是明确的。

+0

谢谢!就是这样。我只需要将'ugettext'更改为'ugettext_lazy',无论我遇到那个错误。幸运的是,只有2例;大多数'ugettext'像以前一样工作。 – benwad