python - 升级到Django 1.7 : Getting AppRegistryNotReady for translation infrastructure

标签 python django gettext django-1.7

我正在从 Django 1.6 升级到 1.7,当我尝试执行 manage.py runserver 时,我得到以下跟踪:

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?或者我需要做一些设置以避免在导入时对其进行评估?

最佳答案

使用 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 来明确说明。

关于python - 升级到Django 1.7 : Getting AppRegistryNotReady for translation infrastructure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34675593/

相关文章:

python - 为 Django 中的简单评论表单添加 CSRF 保护

python - 我可以将 Django runserver 配置为在更改静态或非 python 文件时重新加载吗?

python - 是否可以在 Django Channels Consumer 中使用 ZeroMQ 套接字?

java - 我想交换文本 TextFields

python - Django 十进制字段无法存储应该没问题的 float

javascript - 将python的json对象转换为html表?

python - 如何从 Python3 中给定的值列表中获取字符串中每个字符的值?

django - 在 Django 中管理单独的翻译文件 (.po)

带有 gettext 和 .po 文件的 javascript i18n

python - 自动修复 python 缩进错误