在Django表单中显示红色星号

在Django表单中显示红色星号

本文介绍了在Django表单中显示红色星号(*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表格中更改label_suffix的颜色。

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

我只想将 *设置为红色,其余的保留为黑色。这有可能还是我必须更改HTML中的某些内容?

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?

用户名= Forms.CharField(label =用户名,label_suffix ='*')

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

推荐答案

摆脱掉- label_suffix ='*'。我们将编写一些CSS,在必填字段后显示 *

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将设置一个名为 required 在HTML标签中,然后输入该字段。

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

现在,将这些行放在CSS文件中以显示红色星号:

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

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

这篇关于在Django表单中显示红色星号(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:01