Problems with GAE + django 1.2
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
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>
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.
Looks like you are missing
from django.utils.translation import gettext_lazy as _
but I have no idea why it worked in previous version.
精彩评论