[Django]-Django password field custom class

4👍

You can do this directlly without the init method

 password1 = forms.CharField(widget=forms.PasswordInput(
    attrs={'class':'form-control','type':'password', 'name': 'password','placeholder':'Password'}),
    label='')

0👍

Ngawang13’s answer solved the issue in the questions for me – i.e. the class was set to form-control.

However, to expand on Ngawang13’s answer, if you wanted to use this field in multiple places, you could create a custom field class.

class MyCustomPasswordField(forms.CharField):
    def __init__(self, **kwargs,):
        self.widget = forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password'})
        super().__init__(**kwargs)

Leave a comment