繁体   English   中英

如何在我的 Django 模板上删除“未设置密码”以编辑配置文件

[英]How to remove “No Password set” on my Django template for editing a profile

在我的 Django 模板上,当用户登录并编辑配置文件时,我有一条看起来很奇怪的消息,内容为“未设置密码。未存储原始密码,因此无法查看此用户的密码,但您可以更改密码使用这个表格。” 如何防止它显示在我的模板上?
下面是我的代码
forms.py

class EditProfileForm(UserChangeForm):
    member_id = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Member Id'}))
    first_name = forms.CharField(label='Firstname', required=False, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Firstname'}))
    last_name = forms.CharField(label='Lastname', required=False, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Lastname'}))
    email = forms.CharField(label='Email*', widget=forms.EmailInput(attrs={'class':'form-control', 'placeholder':'Email'}))
    phone1 = forms.CharField(label='Phone Number 1*', widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Phone Number 1'}))
    phone2 = forms.CharField(label='Phone Number 2', required=False, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Phone Number 2'}))

    class Meta():
        model = User
        fields = ('member_id', 'first_name', 'last_name', 'email', 'phone1', 'phone2')

如果您不想使用UserChangeForm的密码行为,那么我将forms.ModelForm 如果您查看UserChangeForm的代码,它实际上并没有做任何其他事情。

class EditProfileForm(forms.ModelForm):
    member_id = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Member Id'}))
    ...

    class Meta():
        model = User
        fields = ('member_id', 'first_name', 'last_name', 'email', 'phone1', 'phone2')

重要的是您明确设置fields ,因此password字段不会出现在表单中。

这是表单域帮助文本

class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField(label=_("Password"),
        help_text=_("Raw passwords are not stored, so there is no way to see "
                    "this user's password, but you can change the password "
                    "using <a href=\"password/\">this form</a>."))

您可以覆盖默认帮助文本

 class Meta:
        help_texts = {
            'password ': _(''),
        }

此外,在您的情况下,将 UserChangeForm 子类化也没有多大意义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM