Python 我的注册视图没有填充Django中的用户表

Python 我的注册视图没有填充Django中的用户表,python,django,Python,Django,我一直在尝试在sane模板上登录和注册,但一直存在问题。求你了,我需要帮助。以下是我的代码: views.py: def register_user(request): if request.user.is_authenticated(): return redirect('home') if request.method == 'POST': rform = RegistrationForm(request.POST) if rf

我一直在尝试在sane模板上登录和注册,但一直存在问题。求你了,我需要帮助。以下是我的代码:

views.py:

def register_user(request):
    if request.user.is_authenticated():
        return redirect('home')
    if request.method == 'POST':
        rform = RegistrationForm(request.POST)
        if rform.is_valid():
            user = User.objects.create_user()
            user.username = rform.cleaned_data['email']
            user.set_password(rform.cleaned_data['password'])
            user.first_name = rform.cleaned_data['first_name']
            user.last_name = rform.cleaned_data['last_name']
            user.email = rform.cleaned_data['email']
            user.gender = rform.cleaned_data['gender']
            user.save()
            loggedin_user = authenticate(email = rform.cleaned_data['email'],
                                          password = rform.cleaned_data['password'])
            if user is not None:
                login(request, loggedin_user)
                return redirect('home')
            else:
                return render(request, 'accounts/access.html', {'rform': RegistrationForm()})
        else:
            return render(request, 'accounts/access.html', {'rform': RegistrationForm()})
    else:
        form = RegistrationForm()
        return render(request, 'accounts/access.html', {'rform':form})


def login_now(request, *args, **kwargs):
    if request.user.is_authenticated():
        return redirect('home')

    if request.method == "POST":
        form = AuthenticationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = authenticate(username = form.cleaned_data['email'], password = password)
            if user is not None:
                login(request, user)
                return redirect('home')
            else:
                return render(request, 'accounts/access.html', {'form': AuthenticationForm(),      'rform':RegistrationForm(), 'next':reverse_lazy('home')})
        else:
            return render(request, 'accounts/access.html', {'form': AuthenticationForm(), 'rform':RegistrationForm(), 'next':reverse_lazy('home')})
    else:
        return render(request, 'accounts/access.html', {'form': AuthenticationForm(), 'rform':RegistrationForm(), 'next':reverse_lazy('home')})
forms.py:

CHOICES = [
('Male', "Male"),
('Female', "Female"),
]

class RegistrationForm(forms.Form):
    first_name = forms.CharField(max_length=25, widget=forms.TextInput(attrs={'placeholder': 'First name'}))
    last_name = forms.CharField(max_length=25, widget=forms.TextInput(attrs={'placeholder': 'Last name'}))
    email = forms.EmailField(max_length=50, widget=forms.TextInput(attrs={'placeholder': 'Email'}))
    password = forms.CharField(max_length=25, widget=forms.PasswordInput(attrs={'placeholder': 'Password'}))
    password1 = forms.CharField(max_length=25, widget=forms.PasswordInput(attrs={'placeholder': 'Password Confirm'}), label=("Re-type Password"))
    gender = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect(attrs={'placeholder': 'Gender'}))

    class Meta:
        model = ('User',)

    def clean_email(self):
        data = self.cleaned_data['email']
        if User.objects.get(email=data):
            raise forms.ValidationError('A user with this email already exist. You may recover the password with a password reset')
        return data

    def clean_password(self):
        password = self.cleaned_data.get("password")
        password1 = self.cleaned_data.get("password1")
        if password1 and password and password1 != password:
            raise forms.ValidationError(
            self.error_messages['password_mismatch'],
            code='password_mismatch',
        )
        return password
access.html:

<div id = 'signup'>
        <form id="post-form" action="{% url 'register' %}" method="POST">
        {% csrf_token %}
        <h3>REGISTER</h3>
            <div>
                {%for field in rform%}
                <div style="margin-top:10px;">
                    {{field.label_tag}}<br/> {{field}}
                    {%if field.errors%} <br/>{{field.errors}} {%endif%}
                </div>
                {%endfor%}
            </div>
                <input type="submit" value="Register" class='sub' id='register'/>
    </div>
    <div id='login'>
    {% if form.errors %}
        {{ form.non_field_errors}}
        {% endif %}
        <form action='{% url 'login' %}' method='post' id ='signIn'>
            {% csrf_token %}
            <h3>SIGN IN</h3>
            <p><label>Email:</label><br/>
            {{ rform.email }}
            {{ rform.email.errors }}</p>
            <p><label>Password:</label><br/>
            {{ rform.password }}
            {{ rform.password.errors }}</p>
            <p><input type="submit" value="login" id='submit' class='sub' /><br>
            <input type="hidden" name="next" value="{% url 'home' %}" />
        </form>
    </div>

{%csrf_令牌%}
登记
{%rform%中的字段为%rform}
{{field.label{u tag}}
{{field} {%if-field.errors%}
{{field.errors}{%endif%} {%endfor%} {%if form.errors%} {{form.non_field_errors}} {%endif%} {%csrf_令牌%} 登录 电子邮件:
{{rform.email} {{rform.email.errors}

密码:
{{rform.password}} {{rform.password.errors}


我试图注册上述所有我总是得到的,是一个重定向,请我需要帮助。 提前谢谢


请注意,我扩展了用户配置文件,这就是为什么我的注册表中有性别。

在您的代码中,您没有比较正确的参数

loggedin_user = authenticate(email = rform.cleaned_data['email'],
                                      password = rform.cleaned_data['password'])
if user is not None:
    login(request, loggedin_user)
    return redirect('home')
您正在检查一个None变量,而不是由authenticate返回的变量
如果loggedin\u用户不是None,则检查是否正确

检查屏幕上的示例

您需要发送已验证的表单
rform
,以便向用户显示反馈(错误),您在验证
RegistrationForm()
后正在发送新表单


还要尝试找出问题的根源,你正在发布你的代码,但你不知道问题来自哪里,至少要做一些调试;)

感谢您的观察和更正,但它仍然没有在我的数据库中注册我的用户