python - 重用 auth 用户更改的密码

标签 python django django-admin

我有以下子类:

class UserProfile(User):
    user = models.OneToOneField(User, parent_link=True)

还有一个 UserProfileAdmin:

class UserProfileAdmin(admin.ModelAdmin):
    # stuff
admin.site.register(UserProfile, UserProfileAdmin)

此管理员在密码字段下显示更改密码链接 /admin/customer/userprofile/1548/password/。但我收到以下错误:

invalid literal for int() with base 10: '1548/password'

我想使用与 auth.User 中相同的更改密码表单,更改后我想重定向到 UserProfileAdmin。我怎样才能做到这一点?

最佳答案

这是预期的行为,因为:

/admin/customer/userprofile/1548/password/

想要显示 ID 为“1548/password”的用户配置文件的更改表单。

扩展 User 类并不是为每个用户存储额外数据的方法。阅读 Storing additional information about users 上的文档了解如何以正确的方式进行操作的说明。

也就是说,如果您希望此网址打开管理员更改密码页面,您可以进行如下重定向:

# put **before** include(admin.site.urls)
url(r'/admin/customer/userprofile/(?P<id>\d+)/password/$', 'views.redirect_to_password'),

在views.py中:

from django import shortcuts

def redirect_to_password(request, id):
    return shortcuts.redirect('/admin/auth/user/%s/password/' % id)

如果您还想将/admin/auth/user/1234 重定向到/admin/customer/userprofile/1234,那么您可以添加以下内容:

url(r'/admin/auth/user/(?P<id>\d+)/$', 'views.redirect_to_customer_changeform'),

可以使用类似的 View :

from django import shortcuts

def redirect_to_customer_changeform(request, id):
    return shortcuts.redirect('/admin/customer/userprofile/%s/' % id)

关于python - 重用 auth 用户更改的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838377/

相关文章:

django - 如何在 Django 管理页面中显示外键的属性

python - 在轴上显示小数位和科学计数法

django - 如何在 Django 中使用 Spyne 的基本身份验证?

python - Django反向查询

python - django - 内联 - 搜索现有记录而不是添加新记录

python - django-admin-sortable 不保存现有对象的顺序

python - 如何在应用程序的源代码管理中管理 python 版本?

python - 使用cgi python区分不同形式

python - 有没有办法在 macOS Big Sur 上使用 SageMath?

Heroku 中的 Django 调试工具栏