简体   繁体   中英

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"})
                              )

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>"),
    ...
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM