2015-02-09 23 views
5

我是Python/Django世界的新手,刚开始一个我很兴奋的大项目。我需要让我的用户通过Facebook登录,我的应用程序具有真正特定的用户流。我设置了django-allauth,一切都按我需要的方式工作。我已覆盖LOGIN_REDIRECT_URL,以便我的用户在登录时登录我要登录的页面。更改django-allauth render_authentication_error行为

但是。当用户打开Facebook登录对话框,然后在没有登录的情况下关闭它,authentication_error.html模板被allauth.socialaccount.helpers.render_authentication_error渲染,这不是我想要的行为。我希望用户只需重定向到登录页面。

是的,我知道我可以简单地将模板放在我的TEMPLATE_DIRS中,但是这个url不会相同。

我得出结论我需要一个中间件拦截对http请求的响应。

from django.shortcuts import redirect 

class Middleware(): 
    """ 
    A middleware to override allauth user flow 
    """ 
    def __init__(self): 
     self.url_to_check = "/accounts/facebook/login/token/" 

    def process_response(self, request, response): 
     """ 
     In case of failed faceboook login 
     """ 
     if request.path == self.url_to_check and\ 
       not request.user.is_authenticated(): 
      return redirect('/') 

     return response 

但我不确定我的解决方案的效率,也不知道pythonesquitude(我认为是这个词)。除了使用中间件或信号之外,还有什么可以改变默认的django-allauth行为吗?

谢谢!

回答

0

我决定用一个中间件和重定向到URL家中情况下,GET请求的形式^/accounts/.*$

from django.shortcuts import redirect 
import re 


class AllauthOverrideMiddleware(): 
    """ 
    A middleware to implement a custom user flow 
    """ 
    def __init__(self): 
     # allauth urls 
     self.url_social = re.compile("^/accounts/.*$") 

    def process_request(self, request): 

     # WE CAN ONLY POST TO ALLAUTH URLS 
     if request.method == "GET" and\ 
      self.url_social.match(request.path): 
      return redirect("/") 
0

是的,我知道我可以简单地通过将模板放在我的TEMPLATE_DIRS中来覆盖模板,但是这个url不会相同。

覆盖模板不会更改URL。在您覆盖的模板中,您可以对任何您喜欢的网址执行client-side redirect

+0

我的意思的URL做的是URL不会是一样的根我想在登录错误时重定向的网址。无论如何,我选择了简单地使用中间件,并将GET请求重定向到/ accounts/*以根URL – 2015-03-11 21:05:17

+0

很酷,很高兴您找到了解决方案。 – 2015-03-11 21:06:29