How to remove required from Django-Widget-Tweaks form field

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

...

2

2 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");
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like