Ale*_*Tea 19 python django django-i18n
当我尝试访问我的应用程序时,我收到以下错误.
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()
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪.
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.
Run Code Online (Sandbox Code Playgroud)
Aje*_*han 30
我遇到了同样的错误.以下为我工作.在您的wsgi文件中,将最后一行更改为:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Run Code Online (Sandbox Code Playgroud)
这已从django 1.6更改为更新版本. 这是有助于部署django应用程序的帖子.
如果你想使用Nginx作为网络服务器来部署django app,请点击这篇文章.
@hellsgate解决方案为我工作.
特别是从@hellsgate引用的链接,我改变了:
module = django.core.handlers.wsgi:WSGIHandler()
Run Code Online (Sandbox Code Playgroud)
至
module = django.core.wsgi:get_wsgi_application()
Run Code Online (Sandbox Code Playgroud)
在我的vassals.ini文件中
这是导致该异常的另一个可能原因:在我的apps.py
文件中,我不小心添加了该类的name
翻译AppConfig
:
class BookConfig(AppConfig):
name = _('Book')
verbose_name = _('Book')
Run Code Online (Sandbox Code Playgroud)
删除错误的翻译后,一切开始完美运行:
class BookConfig(AppConfig):
name = 'Book'
verbose_name = _('Book')
Run Code Online (Sandbox Code Playgroud)
这是不那么聪明的人(像我一样)的答案:一定要检查明显的:错误信息说:... 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
Run Code Online (Sandbox Code Playgroud)
并得到与OP相同的错误,但我的wsgi配置很好.
我所要做的就是更换gettext
有gettext_lazy
(或ugettext
带有ugettext_lazy
)和一切都很好.
归档时间: |
|
查看次数: |
13009 次 |
最近记录: |