Django Widgets set HTML5 maxlength attribute, based on the Model max_length.
class Publication(models.Model):
name = models.CharField(max_length=255)
<input maxlength="255" required="" id="id_name" type="text">
I want to remove this attribute, because is interfering with my own validation, which is more complex.
I know that required attribute can be set to false, but I don't know for other html5 attributes.
I want to apply them in multiple forms to different fields.
Modifying the form init is ok, but not very scalable.
A base class and inheritance is an option, but I don't want to applied to all fields.
I'm looking for something like required=false, to apply to multiple fields.
I was solving this recently. I did this in my forms.py:
from django import forms
class ContactForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=50, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Enter Name'}))
your_surname = forms.CharField(required=False,label='Your surname', max_length=50, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Enter Surname'}))
# Remove html5 attr from input field
your_name.widget.attrs.pop('maxlength', None)
Only for info, this is valid for Forms not ModelForms.