gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:44:57 25 4
gpt4 key购买 nike

我有以下子类:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com