On Friday, 19 July 2013 11:31:27 UTC+2, Tom Evans wrote: > > On Thu, Jul 18, 2013 at 10:31 PM, sephii <[email protected]<javascript:>> > wrote: > > 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 > > I guess the label_tag() method returns a string that is not marked > safe. If you have to do {{ form.name.label_tag }}, you can always do > {{ form.name.label_tag|safe }}. label_tag() is new in 1.5, this may be > a bug. > > Cheers > > Tom >
Hey Tom, Thanks for your answer. I tried your solution but it doesn't work. I checked and label_tag() returns a safe string, otherwise it would not be able to print the <label> tag (because it would be escaped). The problem is with the string that's included in the label, which seems to be escaped twice. Anyway it looks like a bug so I'll just report it. -- 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.

