我使用 django 表单在 django 中创建了一个注册表单,当我运行代码时,出现了一个我没有想到的基于密码的身份验证的字段,我没有使用它而且我不知道它是什么,所以有人可以告诉我它是什么以及如何从用户注册表单中删除它?
表单.py
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.hashers import check_password
class RegisterForm(UserCreationForm):
"""Form to Create new User"""
def __init__(self, *args,hashed_code=None, **kwargs) -> None:
super(RegisterForm,self).__init__(*args, **kwargs)
self.hashed_code = hashed_code
code = forms.CharField(max_length=4,
required=True,
label="code",
help_text="Enter the four-digit code"
)
def is_valid(self):
"""Return True if the form has no errors, or False otherwise."""
if not self.hashed_code:
self.add_error("code","you have not any valid code get the code first")
elif not check_password(self.data.get("code"),self.hashed_code) :
self.add_error("code","code is invalid")
return self.is_bound and not self.errors
class Meta:
model = get_user_model()
fields = ["email", "password1", "password2","code"]