问题描述
我想在我的模板中显示current_user的图片。当我使用django-allauth时,如何访问用户的脸书ID?
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?
推荐答案
对于每个注册用户,社交帐户 SocialAccount
实例可用。该模型具有 User
的外键。请注意,用户可以将多个社交网络帐户连接到其本地帐户,因此实际上可能有多个 SocialAccount
实例可用。
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.
如何处理这是项目依赖。我可以想象有人想要在本地复制个人资料图片,或者您可能希望偏好Facebook上的Google+个人资料图片,等等。
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.
SocialAccount
模型具有一些帮助方法,可让您访问帐户基础,如个人资料图片。总而言之,这是一个快速&肮脏的方式访问第一个可用的配置文件图片:
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}}
ID也可用:
{{user.socialaccount_set.all.0.uid}}
另请参见:
这篇关于如何使用django-allauth获取用户的脸书id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!