Python Django:如何识别用户通过哪个链接进行社会认证?
我是Django开发环境的新手。请帮助我如何在Django应用程序中实现如下场景 在我的网页中,有一些链接可用于使用python social auth模块(用于facebook和google)进行社交注册/登录。在我的系统中有两种类型的用户,TypeA和TypeB 在网页中,每种类型都会有不同的链接(比如A型Facebook登录,B型Facebook登录) 当通过python social auth服务创建用户时,将使用Python Django:如何识别用户通过哪个链接进行社会认证?,python,django,facebook,authentication,python-social-auth,Python,Django,Facebook,Authentication,Python Social Auth,我是Django开发环境的新手。请帮助我如何在Django应用程序中实现如下场景 在我的网页中,有一些链接可用于使用python social auth模块(用于facebook和google)进行社交注册/登录。在我的系统中有两种类型的用户,TypeA和TypeB 在网页中,每种类型都会有不同的链接(比如A型Facebook登录,B型Facebook登录) 当通过python social auth服务创建用户时,将使用django.dispatch.receiver向表TypeAUser或T
django.dispatch.receiver
向表TypeAUser
或TypeBUser
添加一个user
条目
*那么,我如何理解用户是通过哪个链接注册的呢
我尝试过在social auth链接中添加一个额外的参数,但以后如何访问该信息
<a href="{% url 'social:begin' 'facebook' %}?next={{request.path}}&profile_type=typea">
Login with faceboook</a>
非常感谢您的建议和意见。似乎是我自己根据以下文档通过
SOCIAL\u AUTH\u PIPELINE
概念实现的
以下是我所做的修改:
使用自定义管道将SOCIAL\u AUTH\u管道
添加到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'
)
还在settings.py
中添加了存储在会话中的字段,如下所示
FIELDS_STORED_IN_SESSION = ['profile_type']
然后在user\u profile.pipeline.py
中,定义函数create\u user\u profile
,如下所示:
def create_user_profile(strategy, details, user=None, is_new=False, *args, **kwargs):
print(strategy.session_get('profile_type'))
http://domain.name.com/signupurl?profile_type=type1
http://domain.name.com/signupurl?profile_type=type2
现在,我可以从strategy object访问自定义值来创建不同类型的用户配置文件。
在用户单击的主链接上,在url末尾添加了一个额外参数,如下所示:
def create_user_profile(strategy, details, user=None, is_new=False, *args, **kwargs):
print(strategy.session_get('profile_type'))
http://domain.name.com/signupurl?profile_type=type1
http://domain.name.com/signupurl?profile_type=type2
您是如何使用两个版本的用户模型设置contrib.auth的?@khajvah:我没有修改contrib.auth模块,只是添加了新的模型,如TypeAUser
和TypeBUser
,它们将models.OneToOneField('auth.user',primary_key=True)
作为一个用户字段。我认为这不是一个好的解决方案。我建议您创建一个新表UserTypes
,然后从auth.User
向该新表添加一个新外键。或者更简单的解决方案是使用内置的UserGroups
。也就是说,有两个组“A”和“B”,然后给每个用户提供组A或组BI。我不确定python social auth的工作原理。我猜,它会自动创建/验证用户。如果是这样的话,你需要更深入地了解插件本身。我只是指出了一个更大的问题。