Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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='*')

like image 483
Levo Avatar asked Jan 24 '19 19:01

Levo


People also ask

What is the red asterisk in forms?

A red asterisk means that the field is "required" and you won't be able to submit the form without filling that field.

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

How many types of Django forms are there?

Django form fields define two types of functionality, a form field's HTML markup and its server-side validation facilities.


1 Answers

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;
}
like image 126
xyres Avatar answered Oct 23 '22 20:10

xyres