【发布时间】:2020-03-03 06:34:33
【问题描述】:
我的问题 - 如何在 django/contrib/auth/forms.py 中创建自定义文本字段。 ?
我正在尝试调整 Django 默认用户模型。我正在添加一个名为“bio”的测试字段
到目前为止,我在 /python3.6/site-packages/django/contrib/auth/forms.py 文件中的代码如下 -
class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput,
help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput,
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
bio = forms.CharField( #FOO_bio # this value --not getting saved in PSQL
label=_("bio_test within Forms.py"),
#widget=forms.PasswordInput, #Didnt work thus switched to forms.TextInput
widget=forms.TextInput,
strip=False,
help_text=_("Enter some dummy BIO here ."),
)
在此文件中定义的方法中 - def clean_password2(self) 的进一步下方,我正在尝试将“bio”添加为
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
bio = self.cleaned_data.get("bio") # I have no clue how to do this ?
print(bio) #prints None in the terminal
我知道 DICT -cleaned_data 中没有名为“bio”的键。
在文件 - /site-packages/django/contrib/auth/models.py 中,将“简历”添加为 models.TextField ,在类 class AbstractUser(AbstractBaseUser, PermissionsMixin): 中
bio = models.TextField(
_('bio'),
max_length=500, blank=True)
自定义字段“bio”显示在注册表单中,测试用户输入一个文本值 - 表单被提交,但 psql 中没有保存“bio”的任何内容。新用户已注册,可以在终端的 psql 和 Django Admin 中看到。
也在 Django 管理员中 - 当我转到 URL - http://digitalcognition.co.in/admin/auth/user/16/change/ 时,我可以在“个人信息”部分中看到一个名为“Bio”的文本字段,但这又是空白的。 “电子邮件地址”、“用户名”和“密码”与往常一样。
【问题讨论】:
标签: django