Display a red asterisk (*) in Django forms

本秂侑毒 提交于 2020-01-04 03:49:07

问题


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


回答1:


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;
}


来源:https://stackoverflow.com/questions/54353923/display-a-red-asterisk-in-django-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!