GAE 的问题 + Django 1.2

发布于 2024-10-20 11:44:25 字数 2237 浏览 2 评论 0 原文

我升级到 django 1.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

添加新的导入语句后的更新,代码看起来像

   # 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"))

在模板中

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

导致视图上的垃圾,就像这里的图像,其中预期的输出是链接和按钮。您能透露更多信息吗?谢谢

现在检查了HTML,看起来它是带有escpae编码的东西。你能告诉我吗?

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

I upgraded to django 1.2 and I now get this error message which looks related to 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

   # 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

enter image description here

Now inspected the HTML it appears that it's something with the escpae coding. COuld you tell?

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

<li>Log in<a href="google.com">Google</a> <a href="google.com">Yahoo</a> <a href="google.com">MySpace</a> <a href="google.com">AOL</a> <a href="login">Log in</a></li>

</ul>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪之触 2024-10-27 11:44:25

旧版 Django 1.0 手册(App Engine 的默认版本是 0.98 I思考)。

答案如下:

标准翻译:

Python 的标准库 gettext 模块将 _() 安装到全局命名空间中,作为 gettext() 的别名。在 Django 中,我们选择不遵循这种做法,原因如下:

对于国际字符集 (Unicode) 支持,ugettext() 比 gettext() 更有用。有时,您应该使用 ugettext_lazy() 作为特定文件的默认翻译方法。如果全局命名空间中没有 _(),开发人员必须考虑哪个是最合适的翻译函数。

下划线字符(_)用于在Python的交互式shell和doctest测试中表示“先前的结果”。安装全局 _() 函数会造成干扰。显式导入 ugettext() 作为 _() 可以避免这个问题。

这就是为什么旧的可以工作,同时在 Django 1.2 中你需要指定:

from django.utils.translation import gettext_lazy as _

正如 Niklas R 建议的那样。

Found this at Old Django 1.0 manual (App Engine's default version is 0.98 I think).

Here's the answer:

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.

That's why the old one works, meanwhile in Django 1.2 you need to specify:

from django.utils.translation import gettext_lazy as _

as Niklas R suggested.

聊慰 2024-10-27 11:44:25

看起来你失踪了

from django.utils.translation import gettext_lazy as _

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

Looks like you are missing

from django.utils.translation import gettext_lazy as _

but I have no idea why it worked in previous version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文