How to access user names and profiles with django-allauth
Solution 1
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
Solution 2
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
<p>You're logged in with {{ user.get_provider }} as {{ user }}.</p>
<img src="{{ user.get_avatar_url }}" />
Solution 3
you can make for loop in set of socialaccount within foreignkey to user class, in the template it's something like below :
{% for account in user.socialaccount_set.all %}
{% comment %} show avatar from url {% endcomment %}
<h2 style="text-transform:capitalize;">{{ account.provider }} account data</h2>
<p><img width="50" height="50" src="{{ account.get_avatar_url }}"/></p>
<p>UID: <a href="{{ account.extra_data.link }}">{{ account.uid }}</a></p>
<p>Username: {{ account.extra_data.username }}</p>
<p>First Name: {{ account.extra_data.first_name }}</p>
<p>Last Name: {{ account.extra_data.last_name }}</p>
<p>Dashboard Link:
<a href="{{ account.extra_data.link }}">{{ account.extra_data.link }}</a></p>
{% empty %}
<p>you haven't any social account please</p>
{% endfor %}
Richard
Updated on July 19, 2022Comments
-
Richard almost 2 years
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.
-
geewiz over 9 yearsThese get_provider() and get_avatar_url() methods do not seem to be propagating onto the user object with recent django and django-allauth. Seems like this requires manually populating the context.
-
lig over 9 yearsSocialAccount class still has these methods. Are you sure you have SocialAccount instance in context?
-
geewiz over 9 yearsI have the allauth.socialaccount.context_processors.socialaccount context processor active which places a 'socialaccount' dict in the context; the entries in this dict are the active providers. I don't see how this (or the SocialAccount model) modifies the user object, though, permitting access like '{{user.get_provider}}'. Am I missing your point entirely, maybe? Sorry, I'm relatively new to allauth, django, and python so I'm probably missing something obvious.
-
illagrenan almost 7 yearsThis is the correct answer for modern Django (
1.11.x
) and allauth (0.32.x
).