AppRegistryNotReady: 翻译基础设施无法初始化。

28
当我尝试访问我的应用程序时,我遇到了以下错误:

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.

4
在应用程序的 __init__.py 中导入东西时,我遇到了这个错误,最终尝试导入 models.py,而 models.py 又导入了 ugettext - jozxyqk
1
@jozxyqk 谢谢你指出这个问题,我也遇到了同样的错误。将导入从 __init__.py 移动到 models.py 中解决了这个问题。 - Harshveer Singh
10个回答

31

我遇到了同样的错误。以下方法对我有用。 在你的wsgi文件中,将最后一行改为:

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

从Django 1.6版本开始已经有所更改。 这里是帮助部署Django应用的博客文章。

如果您想使用Nginx作为Web服务器来部署Django应用,请参考这篇文章。


1
谢谢,Ajeet。我一直遇到这个问题,通过你的答案解决了它。 - Pragnesh Chaudhari
3
您好。我看不出与作者的wsgi.py文件有任何区别。我正在使用DJango 2,wsgi.py配置与您推荐的相同 :( - Andrés Quiroga

12

这是一个对于不太聪明的人(比如我)的答案:一定要检查显而易见的问题。错误信息说:... 检查是否在导入时进行了非惰性gettext调用。如果您在模型字段的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

我遇到了与原帖相同的错误,但我的wsgi配置没问题。

我所要做的就是用gettext_lazy替换gettext(或者用ugettext_lazy替换ugettext),然后一切都好了。


7
@hellsgate的解决方案对我很有帮助。具体来说,从@hellsgate提供的链接中,我进行了如下修改:
module = django.core.handlers.wsgi:WSGIHandler()

为了

module = django.core.wsgi:get_wsgi_application()

在我的vassals.ini文件中


6
这里还有一个可能引起异常的原因:在我的apps.py文件中,我错误地为AppConfig类的name添加了翻译:
class BookConfig(AppConfig):
    name = _('Book')
    verbose_name = _('Book')

删除错误的翻译后,一切开始完美地运作:

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

4

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

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


3
你可能忘了设置应用程序路径。请查看我的wsgi文件。您可以在这里找到更准确的文档。https://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()

通过这种方式使用sys.path.append,我成功让在http://blog.dscpl.com.au/2008/12/using-modwsgi-when-developing-django.html中引用的wsgi文件正常工作!! - Jesuisme

0
和@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()

0

使用

"from django.utils.translation import gettext_lazy as _"

替代

"from django.utils.translation import gettext as _"

在我的应用程序文件夹中的apps.py文件中解决了我的问题。

0

您可以通过以下方式查看已安装的"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

0
我遇到了与下面相同的错误:
django.core.exceptions.AppRegistryNotReady: 在应用程序注册表准备就绪之前,无法初始化翻译基础设施。请检查您是否在导入时进行了非延迟的 gettext 调用。
这是因为我在 my_app/apps.py 中的 MyAppConfig 类中使用了 gettext() 来表示 verbose_name
# "my_app/apps.py"

from django.apps import AppConfig
from django.utils.translation import gettext as _ # Here

class MyAppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'my_app'
    verbose_name = _('my_app') # Here

我在core/settings.py中使用了gettext()函数,如下所示:

And, I used gettext() in LANGUAGES in core/settings.py as shown below:

# "core/settings.py"

from django.utils.translation import gettext as _ # Here

LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French'))
)

所以,我反而在`my_app/apps.py`中的`MyAppConfig`类中使用`gettext_lazy()`作为`verbose_name`的替代。如下所示,问题得到解决:
# "my_app/apps.py"

from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _ # Here

class MyAppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'my_app'
    verbose_name = _('my_app') # Here

而且,我在core/settings.py中使用了gettext_lazy()LANGUAGES,如下所示,然后错误就解决了:
# "core/settings.py"

from django.utils.translation import gettext_lazy as _ # Here

LANGUAGES = (
    ('en', _('English')), # Here
    ('fr', _('French')) # Here
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接