38531

Display a red asterisk (*) in Django forms

Question:

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

Answer1:

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 <a href="https://docs.djangoproject.com/en/2.1/ref/forms/api/#django.forms.Form.required_css_class" rel="nofollow">required_css_class</a>:

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

Recommend

  • JavaScript - Sum of values in same rows and columns
  • Find when google places autocomplete returns zero results
  • Getting Unable to determine application identity of the caller Error
  • How can I submit iOS apps to the App Store with MacBook A1181?
  • Check if input is positive integer [duplicate]
  • Threading doesn't work correctly
  • Corrupted files uploaded on REST method call
  • Understanding HttpServletRequest and cookies in JSF
  • Drupal field_settings_form
  • FormsAuthentication.CookieDomain property
  • about iOS target version and SDK version
  • Alphabetical array sort - lowercase first
  • Linux could not find metis.h
  • Pass multiple lines of stdin input to interactive Java command line program, non-interactively
  • ClearCase can I use clearexport_ccase/clearimport to copy VOB data to a VOB on a different machine
  • How can I find the difference between two times [duplicate]
  • how to add Image in JPanel
  • How to set http status code when responding to servlet client from Filter class-method in tomcat
  • connect.cookieParser and connect.session
  • How can I run an Illustrator javascript on all files in a directory?
  • Visual Studio MSB3721 error when compiling a __device__ function call from another file
  • Unable to connect to AWS RDS through PDO
  • Validating my form with Jquery
  • NoClassDefFoundError: com.google.gson.GsonBuilder when using google play services with microsoft azu
  • Is possible having two COM STA instances of the same component?
  • Android studio import problems. (Apktool)
  • Checking for valid enum types from protobufs
  • Use AutoIt with java applications
  • how to add dashed border on highcharts “area” graph for every point
  • How to select multiple items from a List view - JavaFX 8
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • C++ STL stack pop operation giving segmentation fault
  • Content-Type alternative in MQTT
  • DataTables move rows between tables
  • How to turn off notice reporting in xampp?
  • how to run ejabberd with Erlang on Heroku?
  • iphone image to video issue in video speed
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • Call Microservice from another Microservice within Docker