升级到Django 1.7:获取AppRegistryNotReady的翻译基础设施 [英] Upgrading to Django 1.7: Getting AppRegistryNotReady for translation infrastructure
问题描述
我正在从Django 1.6升级到1.7,当我尝试执行 manage.py runserver
时,我得到以下跟踪:
I'm upgrading from Django 1.6 to 1.7 and when I try to do manage.py runserver
I get the following trace:
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
:
I haven't used the app registry before, so I assume some setup needs to be done in my apps before translations can be used. The solution I keep seeing is to add this to wsgi.py
:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
但是我已经有那些线在那里在文件 myproject / core / util.py
中我更改了以下行:
However I already have those lines in there. In the file myproject/core/util.py
I changed the following line:
from django.utils.translation import ugettext as _
to:
from django.utils.translation import ugettext_lazy as _
只是将问题移到使用 ugettext
的另一个文件。不可能使用非懒惰的 ugettext
?还是有一些我需要做的设置,以避免在导入时被评估?
And that just moved the issue to another file which used ugettext
. Is it no longer possible to use non-lazy ugettext
? Or is there some setup I need to do to avoid it being evaluated at import time?
推荐答案
使用 make_url(url,text = ugettext('here'))
是在 text
的默认参数被评估为模块导入,而不是当 make_url
函数运行时。
The problem with using make_url(url, text=ugettext('here'))
is that the default argument for text
is evaluated when the module is imported, not when the make_url
function runs.
您还没有显示代码这产生了第二个错误,所以我不知道是什么问题。
You haven't shown the code that generated the second error, so I don't know what was wrong with it.
使用 ugettext
函数内部(只要该函数在导入时不运行)。例如:
It is fine to use ugettext
inside a function (as long as that function doesn't run during import time). For example:
def make_url(url, text=None):
if text is None:
text = ugettext('here')
请注意,您仍然可以执行 import uggettext as _
在你的代码中,我刚刚使用 ugettext
来显示。
Note, you can still do import uggettext as _
in your code, I've just used ugettext
above to be explicit.
这篇关于升级到Django 1.7:获取AppRegistryNotReady的翻译基础设施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!