gpt4 book ai didi

python - 将通过 Python Social Auth 登录的用户添加到组中

转载 作者:太空宇宙 更新时间:2023-11-04 01:17:30 25 4
gpt4 key购买 nike

当用户通过 Python Social Auth 注册时,他们会在我的数据库中创建为用户,这很好,但我希望在创建用户时将他们添加到特定组。

怎么做?

最佳答案

我在尝试使用 python-social-auth 在组中添加用户时遇到了同样的问题

错误:“函数”对象没有连接属性

这是我的 myapp.pipeline.py

from django.db.models import signals
from django.dispatch import Signal
from social.pipeline.user import *

from django.contrib.auth.models import User, Group
from social.utils import module_member

def new_users_handler(sender, user, response, details, **kwargs):
user.groups.add(Group.objects.get(name='candidates'))

user_details.connect(new_users_handler, sender=None)

还有我的 app.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_by_email',
'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',
'companies.pipeline.new_users_handler',
)

所以为了解决这个问题,我做了以下事情:

第 1 步:保留 python-social-auth 管道(没有“companies.pipeline.new_users_handler”)

第二步:在settings.py中添加SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/social/success/'

第 3 步:为/social/success/添加一个 url

第四步:在views.py中添加一个函数,如

@login_required(login_url="/login")
def socialsuccess(request):
defaultgroup = Group.objects.get(name = 'name')
user = request.user
user.groups.add(defaultgroup)

return render(request, 'app/social-success.html')

它工作得很好。但我仍然认为这不是最好的方法。

问候

关于python - 将通过 Python Social Auth 登录的用户添加到组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23482652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com