Search code examples
pythondjangodjango-allauth

Application not able to access the profile pic and posts using DjangoAllAuth


I am trying to access the profile picture and posts of the user using Django-AllAuth. Here is the template where I am trying to load the profile image:

<html>


<body>

 Welcome back {{ user.first_name }} {{ user.age }}
 <img src={{user.cover.source}} height="60" width="60">

<a href="/">Home</a>
</body>


</html>

Here is the view.py

def fb_personality_traits(request):
    # logger.debug('FB Page Loaded')
    return render(request, 'home/facebook_personality_traits.html')

Settings.py

SITE_ID = 1

LOGIN_REDIRECT_URL = '/facebook_personality_traits/'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = {
    'facebook': {
        'SCOPE': ['email', 'user_posts', 'public_profile', 'user_photos'],
        # 'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
        'METHOD': 'js_sdk',
        'FIELDS': [
            'id',
            'email',
            'name',
            'first_name',
            'last_name',
            'cover',
            'posts',
            'age',
        ],
        'EXCHANGE_TOKEN': True,
        'VERIFIED_EMAIL': True,
        'VERSION': 'v2.10',
    }
}
ACCOUNT_LOGOUT_ON_GET = True

Here I am not getting the image. See the below result:

current image

What I want to achieve?

1) I want to display the profile image on the screen.

2) I want to get the posts of the user loggedin in the view.py and send it on the screen to display on the template.


Solution

  • Non-essential fields like age and cover aren't persisted by django-allauth. Only common fields are persisted in the User model automatically (see fields). You would need to handle this yourself, by capturing the data and storing it somewhere, like your database. These fields are only retrieved and accessible on the post-login response (see example method), where they can be found in the extra_data dictionary.

    I'd recommend calling get_avatar_url on the SocialAccount instance (in this case, Facebook) after successful log-in and storing this value in your database. In the case of Facebook, the user avatar URL shouldn't change over time.