如何在python-social-auth中向用户显示有意义的登录错误消息?

时间:2014-12-11 19:10:36

标签: django python-social-auth

我正在使用python-social-auth和Django 1.6,但事情进展顺利。

我将SOCIAL_AUTH_LOGIN_ERROR_URL定义为指向我的应用中的视图,以便在登录未按计划进行时向用户显示错误。

我的系统不允许创建新用户。您必须先使用常规凭据登录您的帐户,将您的帐户与社交帐户相关联,然后才能使用社交身份验证机制登录。

如果用户尝试使用未注册到我系统中用户的社交帐户登录我的系统,我想显示自定义消息。问题是,位于SOCIAL_AUTH_LOGIN_ERROR_URL的视图未收到有关错误可能的任何信息。我能做的最好的事情是“尝试登录时发生错误。”

如果发生特定错误,我如何捕获此类错误并向用户显示自定义消息?

2 个答案:

答案 0 :(得分:1)

看一下django_app中的中间件:

https://github.com/omab/python-social-auth/blob/526439ac66b00ce801e4fc5df89633a66eb8ffb2/social/apps/django_app/middleware.py

并根据此创建您自己的或通过覆盖扩展。如前所述,可能是get_message方法。

答案 1 :(得分:0)

要处理这个问题,我会执行以下操作:

<强> settings.py

...
SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    # the custom pipeline step
    'accounts.views.user_details_after',
)
...

<强> views.py

def user_details_after(strategy, details, user=None, *args, **kwargs):
    if not user:
        messages.info(strategy.request, "You have to sign up first.")

在登录模板中,我将消息呈现给用户。

相关问题