23

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

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

8 回答 8

30

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

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

从 Django 1.6 到更新的版本,这已经改变了。 是帮助部署 django 应用程序的帖子。

如果您想使用 Nginx 作为网络服务器来部署 django 应用程序,请遵循帖子。

于 2015-04-16T06:26:09.800 回答
10

这是对不太聪明的人(比如我)的答案:一定要检查明显的:错误消息说:... 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 配置很好。

我所要做的就是用gettextgettext_lazyugettextugettext_lazy)替换,一切都很好。

于 2017-05-08T23:14:05.280 回答
8

@hellsgate 解决方案对我有用。

特别是从@hellsgate 引用的链接,我改变了:

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

module = django.core.wsgi:get_wsgi_application()

在我的 vassals.ini 文件中

于 2015-08-29T14:09:00.730 回答
4

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

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

于 2015-01-05T16:42:27.367 回答
4

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

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

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

class BookConfig(AppConfig):
    name = 'Book'
    verbose_name = _('Book')
于 2019-11-30T21:06:09.840 回答
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()
于 2015-09-02T14:08:14.533 回答
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
于 2015-03-23T09:42:23.923 回答
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()
于 2017-09-21T23:00:41.573 回答