简体   繁体   English

从python-social-auth获取django用户配置文件

[英]Get django user profile from python-social-auth

My Django project lets users log in with a Google or Facebook profile, using python-social-auth's django integration. 我的Django项目允许用户使用python-social-auth的django集成以Google或Facebook个人资料登录。

Once they've logged in, is there a way to get a link to their profile on Google or Facebook? 他们登录后,是否有办法在Google或Facebook上获得指向其个人资料的链接? I'm trying to do something like this: 我正在尝试做这样的事情:

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()
            )
        )

You can get a link to their Facebook profile before they are logged in. 您可以在登录之前获得指向他们的Facebook个人资料的链接。

In the pipeline , there is a function called social_details which get information about the user from specified social network. pipeline ,有一个名为social_details的函数,该函数从指定的社交网络获取有关用户的信息。

If we take a look at that method, it is pretty simple: 如果我们看一下这种方法,它非常简单:

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

and if you print the response : 并且如果您print response

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

you will see that there is a parameter link , provided login is through Facbook. 您将看到有一个参数link ,前提是通过Facbook登录。 Now you can get that link and save/use it however you want. 现在,您可以获取该link并根据需要保存/使用它。

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

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