Python – Django allauth custom templates do not hash passwords

Django allauth custom templates do not hash passwords… here is a solution to the problem.

Django allauth custom templates do not hash passwords

I’m using my own custom View and inheriting from SignupView (from allauth.account.views import SignupView ) I also use my own forms.py to pass on my custom View. It registers users well, but one thing it doesn’t do is hash passwords. It saves the password for the user as-is. How can I make the password stored as a hash in a table?

Form .py

from .models import User
from django import forms

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:'}))

views.py

from allauth.account.views import SignupView
from .forms import RegisterForm

class RegisterView(SignupView):
    form_class = RegisterForm
    template_name = 'oauth/auth_form.html'

Item urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('oauth.urls')),  //app name I created where my custom sign up view is
    url(r'^profiles/', include('profiles.urls')),

url(r'^accounts/', include('allauth.urls')),
]

Solution

Your form needs to inherit from SignupForm because its save() method uses the correct way to create a new user. When you only use ModelForm, the save() method creates a new User object using the normal initialization of the model, and User creation requires special handling of passwords.

So just define the field and change the 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:'}))

Related Problems and Solutions