How can I get the user's facebook id with django-allauth?

☆樱花仙子☆ 提交于 2019-11-26 19:29:28

问题


I want to display a picture of the current_user in my template. How can I access the user's facebook id when I'm using django-allauth?


回答1:


For each user that is signed up via a social account a SocialAccount instance is available. This model has a foreign key to User. Note that a user can connect multiple social networking accounts to his local account, so in practice there may be more than one SocialAccount instances available.

How you want to deal with this is project dependent. I can imagine one would want to copy the profile image locally, or perhaps you would want to give preference to the Google+ profile picture above the Facebook one, and so on.

The SocialAccount model has some helper methods that give you access to account basics such as the profile picture. All in all, this is a quick & dirty way to access first available profile picture:

{{user.socialaccount_set.all.0.get_avatar_url}}

The ID is also available:

{{user.socialaccount_set.all.0.uid}}

See also: https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py#L72



来源:https://stackoverflow.com/questions/12222021/how-can-i-get-the-users-facebook-id-with-django-allauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!