django-forms

Django Built-in forms

Introduction#

Django is shipped with several views that require forms. These forms are, naturally, built-in. A good example are Authentication Built-in forms.

This topic intends to bring documentation on how to work with these forms.

Add custom CSS classes

Built-in forms are great but sometimes there is a need to customize them, adding new fields or simply changing CSS attributes.

This example is applicable to several use cases but here it is presented regarding PasswordChangeForm and its use in a Bootstrap website.

The solution is to create another Form that inerits PasswordChangeForm update the Widget:

class PasswordChangeCustomForm(PasswordChangeForm):
    def __init__(self, user, *args, **kwargs):
        super(PasswordChangeCustomForm, self).__init__(user,*args, **kwargs)
        for field in self.fields:
            self.fields[field].widget.attrs['class'] = 'form-control'

If you only pretend to change certain fields you may do:

class PasswordChangeCustomForm(PasswordChangeForm):
    def __init__(self, user, *args, **kwargs):
        super(PasswordChangeCustomForm, self).__init__(user, *args, **kwargs)
        self.fields['old_password'].widget.attrs.update({'class': 'form-control'})
        self.fields['new_password1'].widget.attrs.update({'class': 'form-control'})
        self.fields['new_password2'].widget.attrs.update({'class': 'form-control'})

Note: all bootstrap forms require the class form-control to keep the website look and feel.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow