django-成功注册后未设置密码 [英] django - no password set after successful registration

查看:77
本文介绍了django-成功注册后未设置密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 UserCreationForm 创建了一个自定义用户注册表单。当我尝试注册时,它确实注册成功,并且可以看到一个新创建的用户以及用户名及其电子邮件。但是该用户没有密码。

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


推荐答案

使用 super ,使用格式 MyRegistrationForm ,而不是其父类 UserCreationForm

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

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

这篇关于django-成功注册后未设置密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆