I am trying to remove the required attribute from a django-widget-tweaks form.
I tried:
{% render_field form.legal_entity|attr:'required:false' placeholder=form.legal_entity.label class+="form-control" %}and
{% render_field form.legal_entity|remove_attr:"required" placeholder=form.legal_entity.label class+="form-control" %}No matter what I do, the result is always:
<input type="text" name="legal_entity" maxlength="120" placeholder="Firmenname" required>Here is the according Form:
class MerchantForm(forms.ModelForm): class Meta: model = Merchant fields = ['name', 'legal_entity', 'legal_address','legal_zip', 'legal_city','address', 'zip', 'city', 'contact_person', 'phone', 'email'] def clean_legal_entity(self): data = self.cleaned_data['legal_entity'] return data...
22 Answers
You can mark the field as non-required by setting required=False [Django-doc] in the corresponding field:
class MerchantForm(forms.ModelForm): legal_entity = forms.CharField(required=False) class Meta: model = Merchant fields = ['name', 'legal_entity', 'legal_address','legal_zip', 'legal_city','address', 'zip', 'city', 'contact_person', 'phone', 'email'] in the JS code of your HTML page:
window.onload=myfunction(); function myfunction()
{ $("input").removeAttr("required"); $("select").removeAttr("required"); $("textarea").removeAttr("required");
}