简体   繁体   English

GAE + Django 1.2的问题

[英]Problems with GAE + django 1.2

I upgraded to django 1.2 and I now get this error message which looks related to i18n. 我升级到django 1.2,现在得到的错误消息看起来与i18n有关。 Can you tell what I should do? 你能告诉我该怎么办吗? thanks 谢谢

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

UPDATE after having added the new import statement the code looks like 添加新的import语句后的UPDATE代码看起来像

   # let user choose authenticator        
        for p in openIdProviders:
            p_name = p.split('.')[0] # take "AOL" from "AOL.com"
            p_url = p.lower()        # "AOL.com" -> "aol.com"
            loginmsg = loginmsg + '<a href="%s">%s</a> ' % ( #'','')
             #      users.create_login_url(federated_identity=p_url), p_name)
                    'google.com', p_name)
        loginmsg = loginmsg + '<a href=\"%s\">%s</a>' % ('login',_("Log in"))

and in template 并在模板中

    <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>

leading the junk on the view like the image here where the expected output is links and buttons. 像视图中的图像一样在视图上引导垃圾,其中预期的输出是链接和按钮。 Could you inform a bit more? 你能告诉更多吗? Thanks 谢谢

在此处输入图片说明

Now inspected the HTML it appears that it's something with the escpae coding. 现在检查了HTML,看来它带有escpae编码。 COuld you tell? 你能否告诉?

<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>

Looks like you are missing 看起来你失踪了

from django.utils.translation import gettext_lazy as _

but I have no idea why it worked in previous version. 但我不知道为什么它在以前的版本中有效。

Found this at Old Django 1.0 manual (App Engine's default version is 0.98 I think). 旧Django 1.0手册 (我认为App Engine的默认版本是0.98)中发现了此问题。

Here's the answer: 答案如下:

STANDARD TRANSLATION: 标准翻译:

Python's standard library gettext module installs _() into the global namespace, as an alias for gettext(). Python的标准库gettext模块将_()安装到全局名称空间中,作为gettext()的别名。 In Django, we have chosen not to follow this practice, for a couple of reasons: 在Django中,出于以下两个原因,我们选择不遵循此做法:

For international character set (Unicode) support, ugettext() is more useful than gettext(). 对于国际字符集(Unicode)支持,ugettext()比gettext()更有用。 Sometimes, you should be using ugettext_lazy() as the default translation method for a particular file. 有时,您应该使用ugettext_lazy()作为特定文件的默认翻译方法。 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. 下划线字符(_)用于表示Python的交互式shell和doctest测试中的“先前结果”。 Installing a global _() function causes interference. 安装全局_()函数会引起干扰。 Explicitly importing ugettext() as _() avoids this problem. 显式导入ugettext()作为_()可以避免此问题。

That's why the old one works, meanwhile in Django 1.2 you need to specify: 这就是为什么旧版本有效的原因,而在Django 1.2中,您需要指定:

from django.utils.translation import gettext_lazy as _

as Niklas R suggested. 正如Niklas R建议的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM