Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a label bold in a Django form?

How can I make a label bold in a Django form?

The form element goes like this:

condition = forms.TypedChoiceField(label="My  Condition is",
                              coerce= int,
                              choices=Listed.CONDITION,
                              widget=RadioSelect(attrs={"class": "required"})
                              )
like image 609
Ching Chong Avatar asked Jan 17 '12 12:01

Ching Chong


1 Answers

Usually, the easiest way would be to do it in your CSS. label[for="id_condition"]{font-weight:bold;} if you're only dealing with browsers that have attribute selectors implemented. These days, that means everything but IE6. If you do need to support IE6, you can wrap the field in a div and style it that way:

<div class="bold-my-labels">{{ form.condition.label_tag }}{{ form.condition }}</div>
<style type="text/css">.bold-my-labels label{font-weight:bold;}</style>

Lastly, if you need to do it on the Python side of things, you can always stick the HTML in your label arg, a-la "<strong>My Condition is</strong>". But it'll get escaped in the HTML unless you mark it as safe, so you'd end up with:

from django.utils.safestring import mark_safe
...
    condition = forms.TypedChoiceField(
        label=mark_safe("<strong>My Condition is</strong>"),
    ...
    )
like image 113
AdamKG Avatar answered Sep 22 '22 15:09

AdamKG