New line in Django form field label

Is there a way to put a newline character in a form field label in Django? The room \ncauses a new line to appear in the HTML, and when you try <br>, only characters are displayed. Is there an equivalent operation |safethat I can specify in a field?

+5
source share
2 answers

You can use mark_safeto prevent the tag <br />from escaping.

This is equivalent to using safein a template, so be careful if you are handling user input. If it is a hard-coded string, then it is 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'))
+14
source

:

 {{my_field.label|linebreaks}} 

\n <br/>

doc,

newline HTML (<br />) , (</p>).

+2

All Articles