Newline in label for Django form field

后端 未结 2 1378
礼貌的吻别
礼貌的吻别 2021-02-14 02:35

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
just

2条回答
  •  隐瞒了意图╮
    2021-02-14 02:48

    You can use mark_safe so that the
    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
    next line'))

提交回复
热议问题