Aamu
Aamu

Reputation: 3611

django - no password set after successful registration

I have created a custom User registration form, from the UserCreationForm. When I try to register, it does register successfully, and I can see a newly created user with the username and its email. But there's no password for that user.

In the admin, the password field for that user is No password set.. Please correct me where I am wrong. Thank you.

forms.py:

from album.forms import MyRegistrationForm

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class MyRegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2',)

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']

        if commit:
            user.save()

        return user

views.py:

def register_user(request):
    if request.method == "POST":
        form = MyRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success/')
    else:
        form = MyRegistrationForm()
    return render(request, 'register.html', {'form':form})

Upvotes: 3

Views: 2827

Answers (1)

Alasdair
Alasdair

Reputation: 309069

When calling save on the superclass using super, use the form MyRegistrationForm, not its superclass UserCreationForm.

user = super(MyRegistrationForm, self).save(commit=False)

Upvotes: 4

Related Questions