2016-01-08 44 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