python - 我的密码存储在 Django admin 的电子邮件字段内

标签 python django

我有一个存储 2 个表单数据的 View 。我的第二个表格的数据很好。但对于主用户表单,我只想存储用户名、密码并进行密码确认。创建用户时,由于某种原因,密码会存储在电子邮件字段中。

class UserForm(forms.ModelForm):
    password = forms.CharField(label='Password', max_length=32, required=True, widget=forms.PasswordInput)
    confirm_password = forms.CharField(label='Confirm', max_length=32, required=True, widget=forms.PasswordInput, help_text="Passwords must match!")

    def clean(self):
        cleaned_data = super(UserForm, self).clean()
        password = cleaned_data.get("password")
        confirm_password = cleaned_data.get("confirm_password")

        if password != confirm_password:
            raise forms.ValidationError(
                "password and confirm_password does not match"
            )

    class Meta:
        model = User
        fields = ('username', 'password')
        exclude = ('email',)

def student_register(request, user):
    data = dict()
    if request.method == 'POST':
        form1 = UserForm(request.POST)
        form2 = StudentForm(request.POST, request.FILES)
        if form1.is_valid() and form2.is_valid():
            cd1 = form1.cleaned_data
            user.username = cd1["username"]
            user.password = cd1["password"]
            user.confirm_password = cd1["confirm_password"]
            new_user = User.objects.create_user(user.username, password, confirm_password)
            new_user.save()
            cd2 = form2.cleaned_data
            name = cd2['name']
            surname = cd2['surname']
            email = cd2['email']
            phone = cd2['phone']
            student_id = cd2['student_ID']
            photo = cd2['photo']
            Student.objects.create(user=new_user, name=name, surname=surname, email=email, phone=phone,
                                   student_ID=student_id, photo=photo)
            return redirect('index')
    else:
        form1 = UserForm()
        form2 = StudentForm()
    data['form1'] = form1
    data['form2'] = form2
    return render(request, "student_signup_form.html", data)

最佳答案

create_user 的第二个参数方法是电子邮件。更改您的代码,以便将 password 作为关键字参数传递。该行不需要 confirm_password

new_user = User.objects.create_user(user.username, password=password)

关于python - 我的密码存储在 Django admin 的电子邮件字段内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48448563/

相关文章:

python - 如何通过条件选择从 Pandas DataFrame 返回列列表,其中一行中的所有值都是 True?

python - 在 Gensim.Doc2Vec 中应用 Similar 函数

django - 将 Nginx 设置为 Apache 的反向代理与 Apache Event MPM 的反向代理

djangorest框架-对象权限不触发

python - 使用来自第二个应用程序的模型测试 Django 应用程序

Django 静态文件和 Amazon S3 : How to detect modified files?

python - For 循环遍历 2 的幂

python - 同时使用两个 Flask 应用程序

python - 如何在 pandas 中过滤年份和季度

python - Django:用户名的urlpattern?