gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:14:32 26 4
gpt4 key购买 nike

我有一个存储 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com