[Django]-Django allauth custom template not hashing passwords

3👍

Your form needs to inherit from SignupForm because its save() method is using the correct way for creating new users. When you just use a ModelForm, the save() method will create a new User object using normal initialisation of models, whereas User creation needs special treatment for the password.

So just define the fields and change password to password1:

from allauth.account.forms import SignupForm

class RegisterForm(SignupForm):
    username = forms.CharField(label='Username', widget=forms.TextInput(attrs={'placeholder': 'Username:'}))
    email = forms.EmailField(label='Email', widget=forms.EmailInput(attrs={'placeholder': 'Email:'}))
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'placeholder': 'Password:'}))

0👍

You can do by overriding save

from django.contrib.auth.hashers import make_password

class RegisterForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'password']

    username    = forms.CharField(label='Username', widget=forms.TextInput(attrs={'placeholder': 'Username:'}))
    email       = forms.EmailField(label='Email', widget=forms.EmailInput(attrs={'placeholder': 'Email:'}))
    password    = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'placeholder': 'Password:'}))

    def save(self, commit=True):
        instance = super(RegisterForm, self).save(commit=False)
        if instance.password:
            instance.password = make_password(instance.password)
        if commit:
            instance.save()
        return instance

Leave a comment