Django Register View not Saving User Model to Database

26 Views Asked by At

I built a registration form + view + template, but when I try to register, the user is not being saved to the database, neither does it log me in.

I expected my form to register my user and save it to the database. This is my code

(Register form)

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

class UserRegistrationForm(forms.ModelForm):
    email = forms.EmailField()
    password = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['username', 'email']

    def check_password(self):
        if self.cleaned_data['password'] != self.cleaned_data['password2']:
            raise forms.ValidationError('Passwords do not match!')
        return self.cleaned_data['password2']

(Register view)

def register_view(request):
    if request.method == 'POST':
        register_form = UserRegistrationForm(request.POST)
        if register_form.is_valid():
            user = register_form.save(commit=False)
            user.set_password = register_form.cleaned_data['password']
            # Automatically log in the user after registration
            username = register_form.cleaned_data['username']
            password = register_form.cleaned_data['password']
            user = authenticate(request, username=username, password=password)
            if user is not None:
                login(request, user)
                # Redirect to a success page or home page
                return redirect('home1:index')  # Adjust 'home' to your home page URL name
    else:
        register_form = UserRegistrationForm()
    return render(request, 'register.html', {'register_form': register_form})

(Register Template)

<form class="form" method="post">
    {% csrf_token %}
    <label class="label" for="email">Email:</label>
    <input class="input" type="text" id="email" name="email">
    <label class="label" for="username">Username:</label>
    <input class="input" type="text" id="username" name="username">
    <label class="label" for="password">Password:</label>
    <input class="input" type="password" id="password" name="password">
    <label class="label" for="confirm_password">Confirm Password:</label>
    <input class="input" type="password" id="confirm_password" name="password1">
    <button class="button" type="submit">Register</button>
</form>
1

There are 1 best solutions below

0
Vinita Sonakiya On

The form is not registering user because register_form.is_valid() is giving false.

Root cause: form does not contain field password2.

to fix this error change your html like this:

<form class="form" method="post">
    {% csrf_token %}
    <label class="label" for="email">Email:</label>
    <input class="input" type="text" id="email" name="email">
    <label class="label" for="username">Username:</label>
    <input class="input" type="text" id="username" name="username">
    <label class="label" for="password">Password:</label>
    <input class="input" type="password" id="password" name="password">
    <label class="label" for="confirm_password">Confirm Password:</label>
    <input class="input" type="password" id="confirm_password" name="password2">
    <button class="button" type="submit">Register</button>
</form>

To identify these kinds of issues you should always check register_form.errors

You can also print errors on html like this:

{{register_form.errors}}