Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django允许在管理站点中编辑用户密码_Django_Django Admin - Fatal编程技术网

Django允许在管理站点中编辑用户密码

Django允许在管理站点中编辑用户密码,django,django-admin,Django,Django Admin,我知道以前也有人问过类似的问题,但我无法让它起作用 我已覆盖用户管理模板,因此只能显示用户的特定字段。这是有效的 但是我不能编辑用户密码,因为它看起来像是散列的。我想广告的链接,因为它是以前。 我看了一下解决方案,但都不管用。我的代码如下: class UserAdmin(admin.ModelAdmin): fieldsets = ( (None, {'fields': ('username', 'password')}), (_('Personal info'), {'fi

我知道以前也有人问过类似的问题,但我无法让它起作用

我已覆盖用户管理模板,因此只能显示用户的特定字段。这是有效的

但是我不能编辑用户密码,因为它看起来像是散列的。我想广告的链接,因为它是以前。 我看了一下解决方案,但都不管用。我的代码如下:

class UserAdmin(admin.ModelAdmin):
fieldsets = ( 
    (None, {'fields': ('username', 'password')}),
    (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
    (_('Permissions'), {'fields': ('is_active', 'is_superuser')}),
    (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
) 
list_display = ('username', 'email', 'first_name', 'last_name')
list_filter = ('is_active', 'groups')
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>."))

admin.site.unregister(User)
admin.site.register(User,UserAdmin)
class UserAdmin(admin.ModelAdmin):
字段集=(
(无,{'fields':('username','password')}),
((‘个人信息’,{'fields':('first_name','last_name','email')),
(u('Permissions'),{'fields':('is_active','is_superuser')),
((‘重要日期’,{‘字段’:(‘上次登录’,‘加入日期’))),
) 
列表显示=('username'、'email'、'first\u name'、'last\u name')
列表过滤器=(“是否处于活动状态”、“组”)
password=ReadOnlyPasswordHashField(标签=(“密码”),
help_text=(“未存储原始密码,因此无法查看”
此用户的密码,但您可以更改密码
“使用。”)
管理员.站点.注销(用户)
admin.site.register(用户,UserAdmin)
所有这些都很好,但是密码显示为一个简单的字段,而文本不会出现

谢谢

您永远不会“编辑”密码,因为它不是以“清除”方式保存的,而是先加密的

您可以使用
AdminPasswordChangeForm
将链接添加到其他视图

文档

这就是解决方案:

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>."))


我如何添加该链接?我尝试添加问题中显示的字段“password”,但没有成功:在用户管理模板中,添加到
admin/auth/user/{{{user.id}}}/password/
的链接,或者添加到特定页面以更改passowrd
class AdminUserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField(
        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:
        model = User
        fields = (
            'email', 'first_name', 'last_name',
        )

    def clean_password(self):
        ...
        return self.initial["password"]
class UserAdmin(BaseUserAdmin):
    ...
    fieldsets = (
        ('Login', {
            'fields': [
                'email',
                'password',
            ]
        }),
        ...
    )

    ...
    form = AdminUserChangeForm
    add_form = AdminUserCreationForm