0

I was trying to style my password fields in the registration of my Django Accounts App.

'password1': forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password','required': 'required'}, ),
'password2': forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Confirm password','required': 'required'}, ),

My view renders to the register.html which is in the code below

<div class="col-12 mt-4">
    {{ form.password1 }}
</div>
<div class="col-12 mt-4">
    {{ form.password2 }}
</div>

But the result for the Password field is not styled as the other fields. The rendered register page is as shown below
rendered register.html page

What could be the fix to give the following output expected output

3 Answers 3

1

After research, it turns out the default Django register fields i.e "username", "password1" and "password2" widgets can be tweaked at the constructor, here is the working code

def __init__(self, *args, **kwargs):
    super(SignUpForm, self).__init__(*args, **kwargs)

    self.fields['username'].widget.attrs['class'] = 'form-control'
    self.fields['username'].widget.attrs['placeholder'] = 'Username'
    self.fields['password1'].widget.attrs['class'] = 'form-control'
    self.fields['password1'].widget.attrs['placeholder'] = 'Password'
    self.fields['password2'].widget.attrs['class'] = 'form-control'
    self.fields['password2'].widget.attrs['placeholder'] = 'Confirm password'
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest like this: Please try this.

Method 1

password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password','required': 'required'}, ))
password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Confirm password','required': 'required'}, ))

Method 2

Using constructor (Reduce code)

def __init__(self, *args, **kwargs):
    super(SignUpForm, self).__init__(*args, **kwargs)
    self.fields['username'].widget.attrs = {'class': 'form-control', 'placeholder': 'Password','required': 'required'}
    self.fields['password1'].widget.attrs = {'class': 'form-control', 'placeholder': 'Confirm password','required': 'required'}
    self.fields['password2'].widget.attrs = {'class': 'form-control', 'placeholder': 'Confirm password','required': 'required'}

1 Comment

I was able to fix it using the constructor, turns out the default fields from django i.e username, password1 and password2 widgets can be enhanced at the constructor..Here is the code
0

Try using crispy_fields or django-crispy-forms. I faced same issue but after that I implemented crispy-forms and resolved issue. https://django-crispy-forms.readthedocs.io/en/latest/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.