没有HTML转义的Django表单值 [英] Django Form values without HTML escape
问题描述
我需要设置Django forms.ChoiceField来显示货币符号。由于django形式转义所有的HTML ASCII字符,所以我不能得到$ (€ )或磅; (£ )显示货币符号。
I need to set the Django forms.ChoiceField to display the currency symbols. Since django forms escape all the HTML ASCII characters, I can't get the $ ( € ) or the £ ( £ ) to display the currency symbol.
<select id="id_currency" name="currency">
<option value="&#36;">$</option>
<option value="&pound;">£</option>
<option value="&euro;">€</option>
</select>
您可以建议任何方法来显示实际的HTML货币字符,至少对于期权的价值部分?
Could you suggest any methods to display the actual HTML Currency character at least for the value part of the option?
<select name="currency" id="id_currency">
<option value="&#36;">$</option>
<option value="&pound;">£</option>
<option value="&euro;">€</option>
</select>
更新:
请注意,我使用 Django 0.96 ,因为我的应用在Google App Engine上运行。
上面的 < SELECT> 是使用Django Forms呈现的。 / p>
Update:
Please note I use Django 0.96 as my application is running on Google App Engine.
And the <SELECT> above is rendered using Django Forms.
currencies = (('$', '$'),
('£', '£'),
('€', '€'))
currency = forms.ChoiceField(choices=currencies, required=False)
感谢,
Arun。 em>
推荐答案
您可以使用 safe 在模板中或 mark_safe ,
关闭自动转义模板
或使用Unicode字符而不是您的表单中的HTML实体。
You can use "safe" in the template or "mark_safe" in the view, turn off autoescaping in the template, or use Unicode characters instead of HTML entities in your form.
使用 mark_safe
Using mark_safe
from django.utils.safestring import mark_safe
currencies = ((mark_safe('$'), mark_safe('$')),
(mark_safe('£'), mark_safe('£')),
(mark_safe('€'), mark_safe('€')))
使用 autoescape关闭
作为模板中的替代方法,您可以关闭转义代码块。
标签之间的所有内容 {%autoescape off%}
和 {%endautoescape%}
将不要转义。
As an alternative in your template you can turn off escaping for a block of code.
Everything between tags {% autoescape off %}
and {% endautoescape %}
will not be escaped.
使用Unicode字符
以下。在包含您的货币元组合的文件中,将以下行作为第一行或第二行:
When nothing else works try the following. In the file that contains your currencies tuple put the following line as the very first or second line:
# coding=utf-8
然后用你的货币元组放实际的unicode字符:
and then in your currencies tuple put the actual unicode characters:
currencies = (('$', '$'),
('£', '£'),
('€', '€'))
这篇关于没有HTML转义的Django表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!