Django中的条件登录重定向 [英] Conditional login redirect in Django

查看:74
本文介绍了Django中的条件登录重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解LOGIN_REDIRECT_URL,我了解

<form action="?next={{ next|default:"/foo" }}" method="post">

django-registration的login.html模板中的

.这是我想发生的事情:

in django-registration's login.html template. Here's what I want to happen:

  • 如果用户从主页登录, 将它们重定向到包含以下内容的网址 他们的用户名(/lists/[用户名]).

  • If a user logs in from the homepage, redirect them to a URL that contains their username (/lists/[username]).

如果用户从其他任何位置登录 页面,将它们返回到他们的页面 正在查看.

If a user logs in from any other page, return them to the page they were viewing.

twitter.com处理此问题的方法是仅使登录用户的主页消失.我会考虑这样做,而且很容易解决,但是我的主页上仍然有很多有用的东西,而且我不确定我是否希望对经过身份验证的用户将其删除.我宁愿重定向他们.

The way twitter.com handles this is to simply make the homepage go away for logged in users. I would consider doing that, and it would be pretty easy to solve, but my homepage still has useful stuff on it and I'm not sure I want it to go away for authenticated users. I'd rather redirect them.

我当时想我可以在引用了LOGIN_REDIRECT_URL的settings.py中执行条件操作,但是请求对象在settings中不可用(因此我无法访问request.user.username来构建重定向).显然,我无法在模板的django-registration的默认"参数中执行此操作,因为登录前用户名未知.

I was thinking I could do a conditional in settings.py where LOGIN_REDIRECT_URL is referenced, but the request object is not available in settings (so I can't access request.user.username to build the redirect). And obviously, I can't do it in django-registration's "default" parameter in the template because the username isn't known before login.

解决此问题的正确/最佳方法是什么?谢谢.

What's the correct/best way to solve this? Thanks.

更新:根据S. Lott的以下建议,这是我最终使用的(在主页视图中):

Update: Based on S. Lott's suggestion below, this is what I ended up using (in the homepage view):

if request.user.is_authenticated() and not request.session.get('homepage_redir'): 
    request.session['homepage_redir'] = True
    return HttpResponseRedirect(reverse('list_view',args=[request.user.username])) 

首次从首页登录时,用户将被重定向到其个人页面并设置了会话变量.在对主页的后续请求中,不会发生重定向(因为检测到会话var).从任何其他页面重定向登录的代码均不受影响.

On the first login from homepage, the user is redirected to their personal page and a session variable is set. On subsequent requests for the homepage, the redirect does not occur (because the session var is detected). Code for redirecting logins from any other page is not affected.

推荐答案

如果用户从任何其他页面登录, 将它们返回到原先的页面 查看.

If a user logs in from any other page, return them to the page they were viewing.

这也是Django要做的.

This is what Django does anyway.

使用Django已提供的功能检查会话并正确重定向至登录名.

Check for a session and redirect to a login properly, using Django's already provided functions.

如果用户从首页登录, 将它们重定向到包含以下内容的网址 他们的用户名(/列表/[用户名]).

If a user logs in from the homepage, redirect them to a URL that contains their username (/lists/[username]).

这是视图功能的作用.

如果请求会话为空,则他们刚刚登录.在会话中设置一个标志,并返回到所需页面的重定向.确保使用reverse函数-不要在应用程序中的任何位置编码URL.曾经.

If the request session is empty, they just logged in. Set a flag in the session and return a redirect to the desired page. Be sure to use the reverse function -- do not code the URL anywhere in your applications. Ever.

这篇关于Django中的条件登录重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆