bharal
bharal

Reputation: 16224

django - html code in a dict gets escaped

Howdeh!

I have this dict:

PERIOD = [
    (1,"< 3 mnths"),
    (2,"3 - 6mths"),
    (3,"6mths - 1yr"),
    (4,"1 - 2yrs"),
    (5,"> 2yrs"),
]

Which i want to dispay in my django page. Before, I used to have the point braces < instead of the &lt; stuff, but then i realised that not only was i displaying a drop down, but in other parts of the site i was displaying the contents (so, you know, the 3rd item only).

With the above, when i display say the 3rd item only? Everything looks fine. But when i display the dict as a select dropdown? Then django decides to not decode the &gt; things.

Given i send the dict to the template with this:

periodChoice = ChoiceField(label='', choices=PERIOD, widget=forms.Select(attrs={'class':'regDropDown'}))

and in the template i display it like this:

Choose from  {{ theForm.periodChoice }}

how do i get the template do what i want it to do? I tried asking nicely, i even tried a |safe... no luck!

Upvotes: 0

Views: 225

Answers (2)

rantanplan
rantanplan

Reputation: 7460

Try this:

from django.utils.safestring import mark_safe

PERIOD = [
    (1, mark_safe("&lt; 3 mnths")),
    (2,"3 - 6mths"),
    (3,"6mths - 1yr"),
    (4,"1 - 2yrs"),
    (5, mark_safe("&gt; 2yrs")),
]

and leave the rest as it is now.

Upvotes: 2

Igor Chubin
Igor Chubin

Reputation: 64623

Just specify |safe as a filter in the template.

{{ theForm.periodChoice|safe }}

Upvotes: 1

Related Questions