Christopher Reid
Christopher Reid

Reputation: 4484

Adding a general rule to label_suffix in Django forms

I added this snippet into my models.py to override Django form's default behaviour of adding ":" to form labels

from django.forms import BaseForm
orig_init = BaseForm.__init__
def BaseForm_init(*args, **kwargs):
    kwargs.setdefault("label_suffix", "")
    orig_init(*args, **kwargs)
BaseForm.__init__ = BaseForm_init

It has the intended result. But now I am trying, instead of appending '' (nothing), to append a linebreak 'br' so every label is followed by a line break. So the fourth line of the above snippet would look like

      kwargs.setdefault("label_suffix", "<br>")

This should have the effect of doing the below in a specific instance on all form items

message = forms.CharField(widget=forms.Textarea,label = mark_safe('LABEL_NAME<br>'))

instead it prints
as text on the html page... looking at the HTML source you can see why:

 <label for="id_hello">Hello&lt;br&gt;</label>

The python code is converting the triangular brackets into the html code for a triangular bracket which displays it as text.

How can I get it to print:

 <label for="id_hello">Hello<br></label>

Upvotes: 0

Views: 557

Answers (1)

Sohan Jain
Sohan Jain

Reputation: 2377

Have you tried kwargs.setdefault("label_suffix", mark_safe('<br>'))>?

Upvotes: 1

Related Questions