【问题标题】:redirect to last page not working on python social auth重定向到最后一页不适用于 python 社交身份验证
【发布时间】:2015-01-12 23:35:00
【问题描述】:

我正在使用 python social auth 来使用社交登录,但成功登录后我无法重定向到最后一页。

例如,如果我在以下页面 http://localhost:8000/docprofile/14/ 并单击登录按钮,而不是将我重定向到最后一页 http://localhost:8000/docprofile/14/,它会将我重定向到登录页面。

如果我这样说:

<a href="{% url 'social:begin' 'facebook' %}?next={{ request.path }}"> | Login with Facebook</a>

它重定向到登录页面,并且 url 以奇怪的字符结尾:

http://localhost:8000/login/#_=_

我也试过这个:

<a href="{% url 'social:begin' 'facebook' %}?next={{ request.get_full_path }}"> | Login with Facebook</a>

这次它确实采用了 /docprofile/14 的路径,但仍然没有将我重定向回来,而是将我带到带有 url http://localhost:8000/login/?next=/docprofile/14/#_=_ 的登录页面

【问题讨论】:

  • 除了重定向问题之外,身份验证工作正常吗?
  • @omab 是的,身份验证工作正常。只是重定向是问题!
  • 你找到解决这个问题的办法了吗?

标签: python django redirect python-social-auth


【解决方案1】:

我必须做的是从我的 GET 中的查询字符串中删除下一个参数,并修改 html 以将其包含在模板中,如下所示

def home(request):
    c = context()
    if request.GET.get('next'):
        c = context(next=request.GET['next'])

    return render_to_response('home.html',
                              context_instance=RequestContext(request, c))

def context(**extra):
    return dict({
        'available_backends': load_backends(settings.AUTHENTICATION_BACKENDS)
    }, **extra)

我的模板现在从上下文中获取下一个参数,当我登录时重定向工作。

<a class="col-md-2 btn btn-default" name="{{ backend|backend_class }}" href="{% url "social:begin" backend=name %}?next={{next}}">

【讨论】:

    【解决方案2】:

    您可以将其包含在重定向页面模板的头部 -

    <script type="text/javascript">
       if (window.location.hash == '#_=_') {
          window.location.hash = '';
       }
    </script>`
    

    它会成功的!

    【讨论】:

      【解决方案3】:

      解决方案:

      <a class="fb-login-placeholder" href="{% url 'social:begin' 'facebook' %}?next={{ request.GET.next }}">| Login with Facebook</a>
      

      你需要request.GET.next,而不是request.path。
      事实上,要摆脱有趣的角色,请使用@AshishGupta 的答案。

      【讨论】:

        猜你喜欢
        • 2022-10-02
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-21
        相关资源
        最近更新 更多