Django表单字段标签中的换行符 [英] Newline in label for Django form field

查看:506
本文介绍了Django表单字段标签中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Django的表单字段标签中添加换行符?放入 \n 只会在HTML中产生换行符,而尝试< br> 只会显示字符像他们那样。我可以在字段上指定等效的 | safe 操作吗?

解决方案

您可以使用 mark_safe 来避免< br /> 标记被转义。



这等同于在模板中使用 safe ,因此在处理用户输入时请务必小心。



来自django导入形式
来自django.utils.safestring导入mark_safe

类MyForm(forms.Form):
my_field = forms.CharField(label = mark_safe('my label< br />下一行'))


Is there a way to put a newline in the label of a form field in Django? Putting a \n just results in a newline in the HTML, and trying <br> just displays the characters as they are. Is there an equivalent |safe operation that I can specify on the field?

解决方案

You can use mark_safe so that the <br /> tag is not escaped.

It's equivalent to using safe in the template, so be careful if you're handling user input. If it's a hardcoded string, then it's safe to use.

from django import forms
from django.utils.safestring import mark_safe

class MyForm(forms.Form):
    my_field = forms.CharField(label=mark_safe('my label<br />next line'))

这篇关于Django表单字段标签中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆