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
Advertisements

One comment

  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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s