一个关于重定向的 Django 问题
我在 / 上有一个页面,显示用于注册新用户的表单。 当新用户注册时,它将重定向到/dashboard。
但是,当经过身份验证的用户转到 / 时,它应该看到 /dashboard 页面。 问题是:我应该在 home() 视图中放置类似的内容:
if request.user.is_authenticated():
return HttpResponseRedirect("/dashboard")
还是这是一个丑陋方法,我应该根据用户的身份验证状态从 home() 返回不同的模板吗?在这种情况下,如何自定义 URL 以显示 /dashboard 而不是 / ?
I've a page at / that displays a form for signup new users.
When a new user registers, it will redirect to /dashboard.
But when an authenticated user go to /, it should see the /dashboard page.
The question is: should I put something like this in the home() view:
if request.user.is_authenticated():
return HttpResponseRedirect("/dashboard")
or this is an ugly method and should I return different templates from my home() relying on the user's auth status? In this case, how can I customize my URL to show /dashboard and not / ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重定向方法绝对没问题。不知道你说的丑是什么意思。重定向也应该解决您的 URL 问题。
The redirect method is absolutely fine. Not sure what you mean by ugly. The redirect should take care of your URL issue as well.
如果我理解正确的话,您有一个“仪表板”,仅适用于经过身份验证的用户,因此,如果未登录的用户尝试输入它,他应该被重定向到注册表单。正确的?
您考虑过使用中间件吗?此类中间件可以检查用户是否已登录,如果未登录,则返回注册表单视图,否则继续处理请求,并返回主页视图。 此处了解 process_request 和 process_view。
If I understand you correctly, you have a "dashboard" which is available only for authenticated users, so if not logged in user tries to enter it, he should be redirected to the sign up form. Right?
Have you considered using middleware? Such middleware could check if the user is logged in and if not - return the sign up form view, otherwise continue processing the request, and return the home view. Read about process_request and process_view here.