我正在使用python-social-auth
和Django 1.6,但事情进展顺利。
我将SOCIAL_AUTH_LOGIN_ERROR_URL
定义为指向我的应用中的视图,以便在登录未按计划进行时向用户显示错误。
我的系统不允许创建新用户。您必须先使用常规凭据登录您的帐户,将您的帐户与社交帐户相关联,然后才能使用社交身份验证机制登录。
如果用户尝试使用未注册到我系统中用户的社交帐户登录我的系统,我想显示自定义消息。问题是,位于SOCIAL_AUTH_LOGIN_ERROR_URL
的视图未收到有关错误可能的任何信息。我能做的最好的事情是“尝试登录时发生错误。”
如果发生特定错误,我如何捕获此类错误并向用户显示自定义消息?
答案 0 :(得分:1)
看一下django_app中的中间件:
并根据此创建您自己的或通过覆盖扩展。如前所述,可能是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.")
在登录模板中,我将消息呈现给用户。