Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django html format radio select labels

Consider the following structure:

models.py:

class modela(models.Model):
    fielda = models.BooleanField(default=True, choices=((True, 'some <b>bold</b> text'), (False, 'zzz')))

forms.py:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class forma(forms.ModelForm):
    class Meta:
        model = modela
        widgets = {'fielda': forms.RadioSelect}
        fields = '__all__'

views.py:

def a(request):
    form = forma()
    return render(request, 'a.html', {'form': form})

a.html:

{{ form.fielda }}

Django kindly escapes the tags for me as

Django render result

How do I render it as HTML tags like

Expected result

{{ form.fielda | safe }} doesn’t work, either.

>Solution :

Use the django.utils.safestring.mark_safe method to mark the string as safe for outputting in your template

from django.utils.safestring import mark_safe

class modela(models.Model):
    fielda = models.BooleanField(default=True, choices=((True, mark_safe('some <b>bold</b> text')), (False, 'zzz')))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading