Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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})
like image 240
Aamu Avatar asked Dec 04 '25 13:12

Aamu


1 Answers

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

user = super(MyRegistrationForm, self).save(commit=False)
like image 96
Alasdair Avatar answered Dec 07 '25 03:12

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!