简体   繁体   中英

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.

Once they've logged in, is there a way to get a link to their profile on Google or 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.

In the pipeline , there is a function called social_details which get information about the user from specified social network.

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 :

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. Now you can get that link and save/use it however you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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