如何使用django allauth在社交登录后重定向到同一页面?

时间:2016-04-22 07:40:32

标签: django django-allauth

我尝试使用下一个参数,但它不会影响任何事情

这是模板

<div class="col-md-12 text-center">
                    <a href="{% provider_login_url "facebook" method="oauth2" next='{{ request.path }}'%} "><img src = "{% static 'images/fbconnect.png' %}" height="45px" ></a>
                     <a href="{% provider_login_url "google" method="connect" next='{{ request.path }}'%} "><img src = "{% static 'images/g_login.png' %}" height="45px" ></a>
                  </div>

这是视图

if request.POST.get('action') == 'login':
                username_email = request.POST.get('user_email')
                password = request.POST.get('password')
                try:
                    the_user = User.objects.get(username=username_email)
                except:
                    the_user = User.objects.get(email=username_email)

                if the_user is not None:
                    user = authenticate(username=username_email , password=password)
                    if user is not None:
                        login(request , user)
                return HttpResponse(json.dumps({'status' : 'True'}))

1 个答案:

答案 0 :(得分:0)

重定向参数在您的示例中不起作用,因为您没有在视图中使用它,您只是返回HttpResponse。

您应该返回带有您要重定向用户的URL的HttpResponseRedirect。请查看allauth如何在此处实现此功能: https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py

具体来说,请参阅RedirectAuthenticatedUserMixin中的这两行,其中获取并重定向重定向URL:

redirect_to = self.get_authenticated_redirect_url()
response = HttpResponseRedirect(redirect_to)