Remove “required” Attribute on Django 1.10 Forms

I just starting new project with latest version of Django 1.10, the most basic feature and incredibly amazing is django form, that could simplify form rendering and tied up with python object including the validation.

If you realized, django 1.10 has new mechanism for doing input validation. If you are get used using django before version 1.10, all input will validated on server side and throw error message to template, if the input invalid. But since django 1.10, all validation will validated on template first with html5’s “required” attribute by default then it will validated on backend views.

Here is the changelog documentation: https://docs.djangoproject.com/en/1.10/ref/forms/api/#django.forms.Form.use_required_attribute

Here is how to disable this behavior, by removing “required” attribute on rendered form fields.

form = MyForm(use_required_attribute=False)

It’s a bit tricky if you are using class based views with FormView.

class MyFormView(FormView):
    form_class = MyForm
    template_name = 'create_form.html'

    def get_form_kwargs(self):
        kwargs = super(MyFormView, self).get_form_kwargs()
        kwargs['use_required_attribute'] = False
        return kwargs

One response to “Remove “required” Attribute on Django 1.10 Forms”

  1. You can do this more easily in the form class declaration:

    from Django import forms
    
    class MyForm(forms.Form):
        use_required_attribute = False
    

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.