GAE和django 1.2的问题

2024-10-01 09:39:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我升级到django1.2,现在收到这个错误消息,它看起来与i18n有关。你能告诉我该怎么做吗?谢谢

global name '_' is not defined
Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 515, in __call__
    handler.get(*groups)
  File "/base/data/home/apps/classifiedsmarket/blobstore.348713784647505124/i18n.py", line 252, in get
    loginmsg = loginmsg + '<a href=\"%s\">%s</a>' % ('login',_("Log in"))
NameError: global name '_' is not defined

在添加了新的import语句后更新代码

^{pr2}$

在模板中

    <ul><li><a href="ai">{% trans "Add" %}</a></li>
    <li><a href="li">{{ latest.modified|date:"d M" }}</a></li>                  
<li>{% if user %}<a href="{{ user_url|fix_ampersands }}">{% trans "Log out" %}</a>
{% else %}{% trans "Log in" %}{{loginmsg}}{% endif %}</li>
</ul>

引导视图上的垃圾,如此处的图像,其中预期的输出是链接和按钮。你能再告诉我一点吗?谢谢

enter image description here

现在检查了HTML,它似乎是用escape编码的。你能看出来吗?

<ul><li><a href="ai">Add</a></li><li><a href="li">03 Mar</a></li>                   

<li>Log in&lt;a href=&quot;google.com&quot;&gt;Google&lt;/a&gt; &lt;a href=&quot;google.com&quot;&gt;Yahoo&lt;/a&gt; &lt;a href=&quot;google.com&quot;&gt;MySpace&lt;/a&gt; &lt;a href=&quot;google.com&quot;&gt;AOL&lt;/a&gt; &lt;a href=&quot;login&quot;&gt;Log in&lt;/a&gt;</li>

</ul>

Tags: nameinltgtcomlogtransgoogle
2条回答

Old Django 1.0 manual找到这个(appengine的默认版本是0.98)。在

答案如下:

STANDARD TRANSLATION:

Python’s standard library gettext module installs _() into the global namespace, as an alias for gettext(). In Django, we have chosen not to follow this practice, for a couple of reasons:

For international character set (Unicode) support, ugettext() is more useful than gettext(). Sometimes, you should be using ugettext_lazy() as the default translation method for a particular file. Without _() in the global namespace, the developer has to think about which is the most appropriate translation function.

The underscore character (_) is used to represent “the previous result” in Python’s interactive shell and doctest tests. Installing a global _() function causes interference. Explicitly importing ugettext() as _() avoids this problem.

这就是为什么旧的方法有效,同时在Django 1.2中,您需要指定:

from django.utils.translation import gettext_lazy as _

正如Niklas R建议的那样。在

看来你失踪了

from django.utils.translation import gettext_lazy as _

但我不知道为什么它在以前的版本中起作用。在

相关问题 更多 >