Django Register View 未将用户模型保存到数据库

问题描述 投票:0回答:1

我构建了一个注册表单+视图+模板,但是当我尝试注册时,用户没有保存到数据库中,也没有让我登录。

我希望我的表单能够注册我的用户并将其保存到数据库中。 这是我的代码

(登记表)

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']

(注册查看)

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

(注册模板)

<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>
django authentication django-forms django-registration
1个回答
0
投票

表单未注册用户,因为 register_form.is_valid() 给出 false。

根本原因:表单不包含字段password2。

要修复此错误,请更改您的 html,如下所示:

<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>

要识别此类问题,您应该经常检查

register_form.errors

您还可以像这样在 html 上打印错误:

{{register_form.errors}}
© www.soinside.com 2019 - 2024. All rights reserved.