[英]How to prevent Django's label_tag function from escaping the label?
例:
>>> example.label
λ<sub>blabla</sub>
>>> example.label_tag()
[...]&#x3bb;<blabla>[...]
甚至在label_tag()
mark_safe(example.label)
之前调用mark_safe(example.label)
也不会阻止Django转义HTML。 如何让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.
所以
example.label_tag(contents=mark_safe(example.label))
应该工作..我看不出另一种解决这个问题的方法
尝试这个:
from HTMLParser import HTMLParser
h = HTMLParser()
unescaped = h.unescape(example.label_tag())
print unescaped
定义字段时,必须将标签标记为安全。
class MyForm(forms.Form):
example = forms.Field(label=mark_safe('λ<sub>blabla</sub>'))
例:
>>> 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>'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.