【问题标题】:AppRegistryNotReady: The translation infrastructure cannot be initializedAppRegistryNotReady:翻译基础设施无法初始化
【发布时间】:2015-02-22 04:09:58
【问题描述】:

当我尝试访问我的应用时,我收到以下错误。

AppRegistryNotReady:翻译基础设施不能 在应用程序注册表准备好之前初始化。检查你没有 在导入时进行非惰性 gettext 调用

这是我的 wsgi.py 文件:

"""                                                                                                                                                                                     
WSGI config for Projectizer project.                                                                                                                                                    

It exposes the WSGI callable as a module-level variable named ``application``.                                                                                                          

For more information on this file, see                                                                                                                                                  
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/                                                                                                                            
"""

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Projectizer.settings")

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

这里是堆栈跟踪。

mod_wsgi (pid=28928): Exception occurred processing WSGI script '/var/www/projectizer/apache/django.wsgi'.

Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__

    response = self.get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 199, in get_response

    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 236, in handle_uncaught_exception

    return debug.technical_500_response(request, *exc_info)

File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 91, in technical_500_response

    html = reporter.get_traceback_html()

File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 350, in get_traceback_html

    return t.render(c)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 148, in render

    return self._render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 142, in _render

    return self.nodelist.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 844, in render

    bit = self.render_node(node, context)

File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 80, in render_node

    return node.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 90, in render

    output = self.filter_expression.resolve(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 624, in resolve

    new_obj = func(obj, *arg_vals)

File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 769, in date

    return format(value, arg)

File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 343, in format

    return df.format(format_string)

File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 35, in format

    pieces.append(force_text(getattr(self, piece)()))

File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 268, in r

    return self.format('D, j M Y H:i:s O')

File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 35, in format

    pieces.append(force_text(getattr(self, piece)()))

File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 85, in force_text

    s = six.text_type(s)

File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 144, in __text_cast

    return func(*self.__args, **self.__kw)

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 83, in ugettext

    return _trans.ugettext(message)

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 325, in ugettext

    return do_translate(message, 'ugettext')

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 306, in do_translate

    _default = translation(settings.LANGUAGE_CODE)

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 209, in translation

    default_translation = _fetch(settings.LANGUAGE_CODE)

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 189, in _fetch

    "The translation infrastructure cannot be initialized before the "

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.

【问题讨论】:

  • 我在应用程序的__init__.py 中导入内容时遇到了这个错误,该应用程序最终尝试导入models.py,而models.py 又导入了ugettext
  • @jozxyqk 感谢您指出这一点,我遇到了同样的错误,将导入从__init__.py 移至models.py 以解决问题。

标签: python django django-i18n


【解决方案1】:

我遇到了同样的错误。以下为我工作。 在您的 wsgi 文件中,将最后一行更改为:

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

这已经从 Django 1.6 到更新版本进行了更改。 Here 是帮助部署 django 应用的帖子。

如果您想使用 Nginx 作为网络服务器来部署 django 应用,请关注 this 帖子。

【讨论】:

  • 谢谢,阿吉特。遇到这个问题很久了,通过你的回答解决了。
  • 嗨。我看不出与作者的 wsgi.py 文件有什么区别。我正在使用 DJango 2,wsgi.py 配置与您推荐的配置相同:(
【解决方案2】:

这是给不太聪明的人(比如我)的答案:一定要检查明显的:错误消息说:... Check that you don't make non-lazy gettext calls at import time. 所以,如果你在模型字段的 verbose_name 或任何其他字段中使用 django 的翻译在导入时评估的部分,您需要使用*_lazy 版本。如果没有,您最终会遇到 OP 的错误。

我基本上有:

from django.db import models
from django.utils.translation import gettext as _
import datetime
# other things

class myModle(models.Model):
    date = models.DateField(_('Date'), default=datetime.date.today)
    # other defs. and things

并得到与 OP 相同的错误,但我的 wsgi 配置很好。

我所要做的就是将 gettext 替换为 gettext_lazy(或将 ugettext 替换为 ugettext_lazy),一切都很好。

【讨论】:

    【解决方案3】:

    @hellsgate 解决方案对我有用。

    具体从link referenced by @hellsgate,我改了:

    module = django.core.handlers.wsgi:WSGIHandler()
    

    module = django.core.wsgi:get_wsgi_application()
    

    在我的 vassals.ini 文件中

    【讨论】:

      【解决方案4】:

      这似乎与错误报告的错误相同 - https://code.djangoproject.com/ticket/23146

      我也遇到了这个问题,该链接中建议的修复方法对我有用。更新需要在您的wsgi.py 文件中进行。如果您不确定如何进行更改,请发布“wsgi.py”让我查看

      【讨论】:

        【解决方案5】:

        这是导致该异常的另一个可能原因:在我的 apps.py 文件中,我不小心为 AppConfig 类的 name 添加了翻译:

        class BookConfig(AppConfig):
            name = _('Book')
            verbose_name = _('Book')
        

        删除错位的翻译后,一切都开始完美运行:

        class BookConfig(AppConfig):
            name = 'Book'
            verbose_name = _('Book')
        

        【讨论】:

          【解决方案6】:

          您可能缺少设置应用程序的路径。看看我的 wsgi 文件。你会发现更准确的文档herehttps://joshcarllewis.com/articles/getting-started-with-django。我希望它能解决你的问题。

          import os, sys
          
          sys.path.append('D:/django/mysite')
          os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
          
          from django.core.wsgi import get_wsgi_application
          application = get_wsgi_application()
          

          【讨论】:

          【解决方案7】:

          你可以看到你安装了哪个版本的“Django”:

          $python -c 'import django; print (django.get_version ())'
          

          并看到该版本使用项目“requeriments.txt”。如果不是同一个版本,需要卸载“Django”,安装版本设置为“requeriments.txt”。

          如果你使用“虚拟环境”,你可能站错了“虚拟环境”并且你安装了新版本的 django。 例如:

          在您的“requeriments.txt”中放入 Django == 1.6.1

          $python -c 'import django; print(django.get_version())' 
          1.7.4 
          $sudo pip uninstall Django 
          $sudo pip install Django==1.6.1
          

          【讨论】:

            【解决方案8】:

            与@hellsgate 和@shawn 的答案相同。我不得不更换

            import django.core.handlers.wsgi
            application = django.core.handlers.wsgi.WSGIHandler()
            

            通过

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

            【讨论】:

              猜你喜欢
              • 2015-08-27
              • 2016-04-13
              • 1970-01-01
              • 1970-01-01
              • 2011-10-17
              • 2016-04-26
              • 1970-01-01
              • 2023-03-20
              • 1970-01-01
              相关资源
              最近更新 更多