如何使用 django-allauth 访问用户名和配置文件 [英] How to access user names and profiles with django-allauth

查看:45
本文介绍了如何使用 django-allauth 访问用户名和配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Django 和 django-allauth 进行社交认证.

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:

  • 显示登录用户的姓名和头像
  • 向用户的帐户添加一些信息?

例如,在主页上,我有

{% 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?

类似(伪代码):

<p>You're logged in with {{ user.account_provider? }} as {{ user }}.</p>
<img src="{{ user.avatar_url }}" />

那么,如果我想在用户的个人资料中添加额外的属性,我该怎么做?我应该使用其他一些与 Django 用户相关的应用程序吗?

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?

感谢您的帮助.

推荐答案

SocialAccount 模型实例可供使用其社交帐户注册的用户使用.

A SocialAccount model instance is available for users who signed up using their social account.

在你的模板中,你可以简单地写:

In your template, you can simply write:

头像网址: {{ 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源

这篇关于如何使用 django-allauth 访问用户名和配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆