繁体   English   中英

Django:如何识别用户通过哪个链接进行社会认证?

[英]Django: How to identifying through which link user done social authentication?

我是Django开发环境的新手。 请帮助我如何在Django应用程序中实现以下方案。

在我的网页上,有可用python-social-auth模块(适用于facebook和google)进行社交注册/登录的链接。 我的系统中有两种类型的用户:TypeA和TypeB。

在网页中,每种类型都会有不同的链接( 例如TypeA-Facebook-Login,TypeB-Facebook-Login )。

每当通过python-social-auth服务创建User时,都会使用django.dispatch.receiverUser条目添加到表TypeAUserTypeBUser

*那么,我如何了解通过哪个链接用户注册?

我尝试过在社交身份验证链接中添加一个额外的参数,但是以后如何访问该信息?

<a href="{% url 'social:begin' 'facebook' %}?next={{request.path}}&profile_type=typea">
Login with faceboook</a>

以下是我编写@reciver函数的方式。

@receiver(post_save, sender=User,dispatch_uid="add_user_profile")
def add_user_profile(sender,instance, **kwargs):

非常感谢您的建议和意见。

我本人似乎是根据下面的文档参考,通过SOCIAL_AUTH_PIPELINE概念实现了这一SOCIAL_AUTH_PIPELINE

以下是我所做的修改:使用自定义管道将SOCIAL_AUTH_PIPELINE添加到setting.py

# social authentication pipeline
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.user.get_username',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
# custom pipeline
    'user_profile.pipeline.create_user_profile'
)

还添加FIELDS_STORED_IN_SESSIONsettings.py ,如下

FIELDS_STORED_IN_SESSION = ['profile_type']

然后在user_profile.pipeline.py ,定义函数create_user_profile如下:

def create_user_profile(strategy, details, user=None, is_new=False, *args, **kwargs):
    print(strategy.session_get('profile_type'))

现在,从策略对象中,我可以访问自定义值来创建不同类型的用户配置文件。 参考链接

在用户单击的主链接上,在url的末尾添加一个额外的参数,如下所示:

http://domain.name.com/signupurl?profile_type=type1
http://domain.name.com/signupurl?profile_type=type2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM