Django-allauth retrieves avatar profile images

I use django-rest-auth and django-allauth about user registration / login using Facebook user profile.

Now I can take some standard information from Facebook (default for all auth).

Can I also get a list of avatar images? How to do this in allAuth?

+7
django facebook-graph-api django-allauth django-rest-auth
source share
2 answers

If you want to get avatar images in a django template:

 <img src="{{ user.socialaccount_set.all.0.get_avatar_url }}" /> 

Is that what you tried to do?

+14
source share

Let me quickly describe how I did it (using all_auth and rest_auth).

Basically, 'allauth => socialaccount' provides user_signed_up and user_logged_in. So you need to catch them and get sociallogin add extra data and fill in your url avatar.

Step-1: Create a UserProfile model: (to store the avatar_url)

 #models.py try: from django.utils.encoding import force_text except ImportError: from django.utils.encoding import force_unicode as force_text class UserProfile(models.Model): user = models.OneToOneField(User, primary_key=True, verbose_name='user', related_name='profile') avatar_url = models.CharField(max_length=256, blank=True, null=True) def __str__(self): return force_text(self.user.email) class Meta(): db_table = 'user_profile' 

Step-2: Catch the 'user_signed_up' signal and fill in the 'avatar_url'

 # signals.py from allauth.account.signals import user_signed_up, user_logged_in @receiver(user_signed_up) def social_login_fname_lname_profilepic(sociallogin, user): preferred_avatar_size_pixels=256 picture_url = "http://www.gravatar.com/avatar/{0}?s={1}".format( hashlib.md5(user.email.encode('UTF-8')).hexdigest(), preferred_avatar_size_pixels ) if sociallogin: # Extract first / last names from social nets and store on User record if sociallogin.account.provider == 'twitter': name = sociallogin.account.extra_data['name'] user.first_name = name.split()[0] user.last_name = name.split()[1] if sociallogin.account.provider == 'facebook': f_name = sociallogin.account.extra_data['first_name'] l_name = sociallogin.account.extra_data['last_name'] if f_name: user.first_name = f_name if l_name: user.last_name = l_name #verified = sociallogin.account.extra_data['verified'] picture_url = "http://graph.facebook.com/{0}/picture?width={1}&height={1}".format( sociallogin.account.uid, preferred_avatar_size_pixels) if sociallogin.account.provider == 'google': f_name = sociallogin.account.extra_data['given_name'] l_name = sociallogin.account.extra_data['family_name'] if f_name: user.first_name = f_name if l_name: user.last_name = l_name #verified = sociallogin.account.extra_data['verified_email'] picture_url = sociallogin.account.extra_data['picture'] user.save() profile = UserProfile(user=user, avatar_url=picture_url) profile.save() 

Hope this helps you.

+6
source share

All Articles