Example:
>>> example.label
λ<sub>blabla</sub>
>>> example.label_tag()
[...]&#x3bb;<blabla>[...]
Even calling mark_safe(example.label)
before label_tag()
does not prevent Django from escaping the HTML. How can I get label_tag()
to return unescaped labels?
There is a comment in the code for label_tag
Wraps the given contents in a <label>, if the field has an ID attribute.
contents should be 'mark_safe'd to avoid HTML escaping. If contents
aren't given, uses the field's HTML-escaped label.
So
example.label_tag(contents=mark_safe(example.label))
Should work.. I can't see another way around this problem
Try this:
from HTMLParser import HTMLParser
h = HTMLParser()
unescaped = h.unescape(example.label_tag())
print unescaped
You have to mark the label as safe when you define the field.
class MyForm(forms.Form):
example = forms.Field(label=mark_safe('λ<sub>blabla</sub>'))
Example:
>>> f = MyForm({'example': 'foo'})
>>> str(f)
'<tr><th><label for="id_example">λ<sub>blabla</sub>:</label></th><td><input id="id_example" name="example" type="text" value="foo" /></td></tr>'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.