Trouble with accents when passing tranlsated string with newforms

87 views
Skip to first unread message

Tipan

unread,
Mar 8, 2009, 7:55:45 AM3/8/09
to Django users
I'm trying to pass a translated string to a select box using a Forms
Select widget and it always presents the output string with all the
accent HTML entity codes.

In my models file, I have a range of subjects:

Models.py
from django.utils import translation
from django.utils.translation import ugettext_lazy as _

SUBJECT = (
(10, _('Log-in or Account related problems')),
(20, _('General enquiry')),
(30, _('Other feedback')),
)

This is used in a form:

class HelpFeedbackForm(forms.Form):
#form for user to submit their feedback when not logged in
subject = forms.CharField(widget=forms.Select(choices=SUBJECT))

and presented in the template as:

{{ form.subject }}

The translations are returned from the django.po file, but all of the
HTML entity codes are returned as is in the Select box as shown below.
If also tried using the safe template filter tag but to no avail.

"Problemas para el Inicio de Sesión o relacionados con la
Cuenta"


I'm sure I'm doing something silly, but having tried a number of
options, I need a fresh viewpoint. Could anyone suggest how I might
overcome the problem?

Thanks,

Tim









I may be doing something silly, but I can't find the solution

Malcolm Tredinnick

unread,
Mar 8, 2009, 7:07:09 PM3/8/09
to django...@googlegroups.com
On Sun, 2009-03-08 at 04:55 -0700, Tipan wrote:
> I'm trying to pass a translated string to a select box using a Forms
> Select widget and it always presents the output string with all the
> accent HTML entity codes.
>
> In my models file, I have a range of subjects:
>
> Models.py
> from django.utils import translation
> from django.utils.translation import ugettext_lazy as _
>
> SUBJECT = (
> (10, _('Log-in or Account related problems')),
> (20, _('General enquiry')),
> (30, _('Other feedback')),
> )
>
> This is used in a form:
>
> class HelpFeedbackForm(forms.Form):
> #form for user to submit their feedback when not logged in
> subject = forms.CharField(widget=forms.Select(choices=SUBJECT))
>
> and presented in the template as:
>
> {{ form.subject }}
>
> The translations are returned from the django.po file, but all of the
> HTML entity codes are returned as is in the Select box as shown below.

Do you mean they are inserted into the source as "ó" and so
the user sees, literally, "ó"?

> If also tried using the safe template filter tag but to no avail.
>
> "Problemas para el Inicio de Sesión o relacionados con la
> Cuenta"
>
>
> I'm sure I'm doing something silly, but having tried a number of
> options, I need a fresh viewpoint. Could anyone suggest how I might
> overcome the problem?

This could be a bug. Any strings provided by Django itself (and this
includes translated strings, although we don't make that clear in the
documentation) should be automatically considered "safe" in the
auto-escaping sense. That is, they should be inserted literally. If
we're auto-escaping them, it's a bug. I thought we'd flushed out all of
those cases; apparently not.

I'll make some time to investigate this a bit more, but if you can be
bothered to open a ticket describing the problem, that would be helpful
so that we don't forget it.

Regards,
Malcolm


Tipan

unread,
Mar 9, 2009, 10:55:01 AM3/9/09
to Django users

> Do you mean they are inserted into the source as "ó" and so
> the user sees, literally, "ó"?
>

Yes. The string has the html code for the accent as you show above.

> This could be a bug. Any strings provided by Django itself (and this
> includes translated strings, although we don't make that clear in the
> documentation) should be automatically considered "safe" in the
> auto-escaping sense. That is, they should be inserted literally. If
> we're auto-escaping them, it's a bug. I thought we'd flushed out all of
> those cases; apparently not.
>
> I'll make some time to investigate this a bit more, but if you can be
> bothered to open a ticket describing the problem, that would be helpful
> so that we don't forget it.

I'll raise a ticket for this as a bug as you suggest.

I'm experiencing a similar issue when I pass a unicode translated
string for use as a Subject in the send_mail function. It also
presents the text still containing the á etc.

Thanks, Tim

Malcolm Tredinnick

unread,
Mar 9, 2009, 10:15:39 PM3/9/09
to django...@googlegroups.com
On Mon, 2009-03-09 at 07:55 -0700, Tipan wrote:
>
> > Do you mean they are inserted into the source as "ó" and so
> > the user sees, literally, "ó"?
> >
>
> Yes. The string has the html code for the accent as you show above.

I showed two possiblities. Which one is it? If the string is being
inserted as ó then there's no problem here.

Regards,
Malcolm


Malcolm Tredinnick

unread,
Mar 9, 2009, 11:08:43 PM3/9/09
to django...@googlegroups.com

I had some time to test, using the example from #10449 and it is indeed
producing the incorrect result ("ó"). So we'll fix it at some
point.

Regards,
Malcolm


Reply all
Reply to author
Forward
0 new messages