从pythonsocialauth获取Django用户配置文件

2024-03-29 08:55:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Django项目允许用户使用Google或Facebook的个人资料登录,使用pythonsocialauth的Django集成。在

他们有没有登录到谷歌的个人资料?我正试着做这样的事情:

from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.core.mail import mail_admins

@receiver(post_save, sender=User)
def alert_new_user(sender, **kwargs):
    instance = kwargs['instance']
    social = instance.social_auth.last() # Assuming .last() gives me the one just added
    if social is not None:
        mail_admins('New user resistration',
            'A new user registered with name {}'.format(instance.get_full_name()),
            '<html><head/><body>A new user, <a href="{}">{}</a>, registered.</body></html>'.format(
                ???What goes here???, instance.get_full_name()
            )
        )

Tags: djangoinstancenamefromimportnewmodelssave
1条回答
网友
1楼 · 发布于 2024-03-29 08:55:16

在他们登录之前,你可以获得他们Facebook个人资料的链接。在

pipeline中,有一个名为social_details的函数,它从指定的社交网络获取有关用户的信息。在

如果我们看看这个方法,它非常简单:

def social_details(backend, details, response, *args, **kwargs):
    return {'details': dict(backend.get_user_details(response),
                            **details)}

如果你print这个response

^{pr2}$

您将看到有一个参数link,前提是通过Facbook登录。现在您可以获得link并根据需要保存/使用它。在

相关问题 更多 >