简体   繁体   中英

Display a red asterisk (*) in Django forms

I want to change in my form the color of the label_suffix.

I just want to set the '*' in red and leave the rest black. Is this possible or do i have to change something in my HTML?

username = forms.CharField(label="Username",label_suffix='*')

Get rid of this - label_suffix='*' . We'll write some CSS to display a * after the required fields.

First, in your form set an attribute called required_css_class :

class MyForm(...):
    required_css_class = 'required'

Django will set a class called required in the HTML label and input for the field.

Now, put these lines your css file to display a red asterisk:

label.required::after {
    content: ' *';
    color: red;
}

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