如何使用 django-allauth 访问用户名和配置文件
我使用 Django 和 django-allauth 进行社交身份验证。
我已启动并运行身份验证,但任何人都可以提供简单的示例来说明如何:
- 显示已登录用户的名称和头像
- 向用户帐户添加一些信息?
例如,在主页上,我已经
{% if user.is_authenticated %}
<li><a href="{% url account_logout %}?next=/">Logout</a></li>
{% endif %}
正确显示了注销链接,但是我如何添加用户的姓名和头像?
类似于(伪代码):
<p>You're logged in with {{ user.account_provider? }} as {{ user }}.</p>
<img src="{{ user.avatar_url }}" />
那么,如果我想向用户的个人资料添加额外的属性,我该怎么办?我应该使用其他 Django 用户相关的应用程序吗?
感谢您的帮助。
I'm using Django with django-allauth for social authentication.
I have authentication up and running, but can anyone give simple examples of how to:
- show the name and avatar of a logged-in user
- add some information to a user's account?
For example, on the home page, I've got
{% if user.is_authenticated %}
<li><a href="{% url account_logout %}?next=/">Logout</a></li>
{% endif %}
That's showing the Logout link correctly, but how would I add the user's name and avatar?
Something like (pseudocode):
<p>You're logged in with {{ user.account_provider? }} as {{ user }}.</p>
<img src="{{ user.avatar_url }}" />
Then, if I want to add extra properties to the user's profile, what do I do? Should I be using some other Django user-related app?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SocialAccount
模型实例可供使用其社交帐户注册的用户使用。在模板中,您只需编写:
头像网址:
{{ user.socialaccount_set.all.0.get_avatar_url }}
UID:
{{ user.socialaccount_set.all.0.uid }}
加入日期:
{{ user.socialaccount_set.all.0.date_joined}}
上次登录:
{{ user.socialaccount_set.all.0.last_login}}
全名:
{{ user.socialaccount_set .all.0.extra_data.name }}
了解更多信息:Django allauth 源
A
SocialAccount
model instance is available for users who signed up using their social account.In your template, you can simply write:
Avatar URL:
{{ user.socialaccount_set.all.0.get_avatar_url }}
UID:
{{ user.socialaccount_set.all.0.uid }}
Date Joined:
{{ user.socialaccount_set.all.0.date_joined}}
Last Login:
{{ user.socialaccount_set.all.0.last_login}}
And for Full Name:
{{ user.socialaccount_set.all.0.extra_data.name }}
For more information: Django allauth source
如果您查看 django-allauth 源 https: //github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py#L7
这是一个抽象模型代表所有其他特定服务模型具有的所有方法。因此你可以写
If you look at django-allauth source https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py#L7
This is an abstract model that represents all the methods all other specific service models have. Thus you could write
您可以在外键到用户类的社交帐户集中进行 for 循环,在模板中如下所示:
you can make for loop in set of socialaccount within foreignkey to user class, in the template it's something like below :
所以我昨天也在这里得到了库存,我已经在互联网上寻找解决方案,但没有一个看起来像我想出的那样可以理解......
安装并配置 allauth 包后,我执行了以下操作:
在应用程序目录中创建一个 signal.py 文件
在 venv 中找到 allauth 包
然后导航到 allauth/socialaccount/models.py
这样您可以更好地了解如何从 allauth 包中获取模型或调用其他函数和类
在 app/signal.py 中执行以下操作:
您会看到信号的发送者是模型“SocialAccount”,因为该模型始终在身份验证期间创建,然后获取字段“extra_data”(它是一个字典),然后获取键“[ 'picture']”来自“extra_data”字段中的 JSON 对象...我希望这对您有用。谢谢
这样您就可以获取图片的值并为用户分配配置文件
So i got stock here too yesterday, i have gone round the internet for solution but none was looking as understandable as what i came up with...
After installation and configuration of the allauth package, i did the following:
create a signal.py file in app directory
Inside your venv locate the allauth package
Then navigate to allauth/socialaccount/models.py
This way you can have a better understanding on how to grab models from the allauth package or call other functions and classes
In app/signal.py do this:
You see that the sender of the signal is the model "SocialAccount", because this model is always created during authentication, then grab the field "extra_data" which is a dictionary and then the key "['picture']" from the JSON object in the field "extra_data"... I hope this works for you. Thank you
This way you are able to grab the values of the picture and also assign the user with a profile