How to make a shortcut a shortcut in a Django form?

How to make a label bold in a Django form?

The form element is as follows:

condition = forms.TypedChoiceField(label="My  Condition is",
                              coerce= int,
                              choices=Listed.CONDITION,
                              widget=RadioSelect(attrs={"class": "required"})
                              )
+5
source share
1 answer

Usually the easiest way would be to do this in your CSS. label[for="id_condition"]{font-weight:bold;}if you only deal with browsers that have attribute selectors. Nowadays, that means everything except IE6. If you need to support IE6, you can wrap the field in a div and create it like this:

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

Finally, if you need to do this on the Python side, you can always embed HTML in your arg, a-la shortcut "<strong>My Condition is</strong>". But it will escape from HTML unless you mark it as safe, so in the end you will get:

from django.utils.safestring import mark_safe
...
    condition = forms.TypedChoiceField(
        label=mark_safe("<strong>My Condition is</strong>"),
    ...
    )
+8
source

All Articles