Hello,
I'm trying to output a simple form but the label_tag() method tries to
escape the field label twice. Here's what I have:
# models.py
# _ is ugettext_lazy, if it matters
class MyModel(models.Model):
name = models.CharField(_('My label'))
# forms.py
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
# views.py
def my_view(request):
myform = MyForm()
assert False, myform['name'].label_tag()
Now let's say "My label" is translated as "My 'label'" (with single quotes
around it), here's what I get:
<label>My &#39;label&#39;</label>
As you can see the single quotes are escaped twice. I tried to mark_safe()
the label in my model but then the string is not i18n'ed anymore. Also I
found a bugreport in a Django-based project that seems related but I don't
really understand the fix:
https://github.com/stephenmcd/mezzanine/pull/682. I tried using ugettext
instead of ugettext_lazy in my model but then the labels are not i18n'ed
anymore. The only solution that seems to work is to redefine the field in
MyForm and set its label using ugettext instead of ugettext_lazy, but that
seems really complicated given the simple task I'm trying to achieve (also
I don't know in advance which fields could have "special" entities like
quotes).
If I output the form just using {{ form }}, the label is displayed
correctly. Also {{ form.name.label }} is displayed correctly. {{
form.name.label_tag }} is the only method causing the problem. So what am I
doing wrong? I'm using Django 1.5.
Thanks,
Sylvain
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.