Django - 登录后,将用户重定向到他的自定义页面 -->mysite.com/用户名 [英] Django - after login, redirect user to his custom page --> mysite.com/username
问题描述
默认情况下,django 登录后会将用户重定向到帐户/个人资料页面,或者如果您编辑 LOGIN_REDIRECT_URL,您可以将用户发送到您在 settings.py 中指定的另一个页面.
By default after login django redirects the user to an accounts/profile page or if you edit the LOGIN_REDIRECT_URL you can send the user to another page you specify in the settings.py.
这很好,但我希望用户(登录后)被重定向到一个自定义页面,该页面的链接看起来像这样:mysite.com/username
.因此,默认帐户/配置文件或 LOGIN_REDIRECT_URL 设置在这种情况下不起作用,因为两者都是静态的.在我的情况下,地址的 username
部分针对每个用户而变化.
This is great but I would like the user (after login) to be redirected to a custom page where the link to that page would look something like this: mysite.com/username
. So the default accounts/profile or the LOGIN_REDIRECT_URL settings would not work in this case since both are somehow static. In my case the username
section of the address changes for every user.
有什么想法可以让我在用户登录时转到一个自定义用户页面,该页面在地址中包含用户名,例如:mysite.com/username
?任何输入都非常感谢.
Any ideas how I can make it so when the user is logged in would go to a custom user page that has user's name in the address like: mysite.com/username
? Any input is truly appreciated.
推荐答案
您可以按照此处所述对用户进行身份验证和登录:https://docs.djangoproject.com/en/dev/topics/auth/default/#how-to-log-a-user-in
You can authenticate and log the user in as stated here: https://docs.djangoproject.com/en/dev/topics/auth/default/#how-to-log-a-user-in
这将使您能够访问 User 对象,您可以从中获取用户名,然后对自定义 URL 执行 HttpResponseRedirect.
This will give you access to the User object from which you can get the username and then do a HttpResponseRedirect to the custom URL.
这篇关于Django - 登录后,将用户重定向到他的自定义页面 -->mysite.com/用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!