Required class attribute in django ModelForm
You can also be interested in:
Days ago' I was developing a front-end form in order to give users the opportunity to subscribe to a newsletter. I used the django ModelForm class because I didn't need nothing special except from hiding some model fields.
But I run into a problem, the compulsory fileds where handled properly in the sense that the form submission returned an error if such fields weren't filled, but there wasn't any attribute assigned to the label element in order to style it different from non-compulsory fields.
Nothing special here, nor complicated, but it costs me a while to find the reason for this, so I'll share it with you. The simple solution is to add a line (
required_css_class = 'required'
) into the form class:
class subscriptionform(forms.ModelForm):
required_css_class = 'required'
class meta:
model = subscription
exclude = ['categories','code','notes',]
This way if you renders the form as a table the tr element which contains the compulsory field gets a required class.
That's it, nothing special, just a tip.
Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!
Featured

Django admin and bootstrap 5
Bootstrap 5 has come, currently in beta release, and seems already very stable.
So the question is: are you looking for ...

About code optimization, learn from exercises
Let's see an example of exercise you can face during a job interview, and let's use it to understand some ...

Notes on the Pearson correlation coefficient
The Pearson correlation coefficient is a measure of the linear correlation between two variables X and Y. It has a ...
Archive
- 2021
- 2020
- 2019
- 2018
- 2017
- Nov
- Oct
- Aug
- Jun
- Mar
- Feb
- 2016
- Oct
- Jun
- May
- Apr
- Mar
- Feb
- Jan
- 2015
- Nov
- Oct
- Aug
- Apr
- Mar
- Feb
- Jan
- 2014
- Sep
- Jul
- May
- Apr
- Mar
- Feb
- Jan
- 2013
- Nov
- Oct
- Sep
- Aug
- Jul
- Jun
- May
- Apr
- Mar
- Feb
- Jan
- 2012
- Dec
- Nov
- Oct
- Aug
- Jul
- Jun
- May
- Apr
- Jan
- 2011
- Dec
- Nov
- Oct
- Sep
- Aug
- Jul
- Jun
- May