This tutorial is an extension to my previous django allauth tutorial which was a step by step guide to show you how to setup django allauth by Raymond Penners. This tutorial explains how to access the extra data passed by a social account provider to allauth.

I have come across this situation many times where i had a lot of friends, colleagues and djangoers asking me How to get access to the Extra Data passed to django allauth by social providers in our templates. or how do i retrieve the extra information from social accounts using django-allauth and save it in the User Profile model.

Well, there are various different approaches to this, but the first thing that you need to know is that django allauth by default saves all the information [the JSON data dictionary / Response] received by the social account providers in a field called extra_data under its SocialAccount Model. So the good news is the data you need has been already saved for you by django allauth (that’s the beauty of it). But how do we exactly get that data to show up in our templates? What template tags do we use?

Well Below is an easy peasy way of accessing all that information directly in your template.

Access Django Allauth Extra Data in Templates

[This method may not be best method as it is just for informational purpose. A better way to achieve this would be filtering out all the information you need at the view level and then pass it to the template.]

  1. So, Let’s start off from where we left in the last tutorial. We had a sampleapp that was now able to let us login through a social account (in my case it was facebook).
  2. By default after creating / authenticating a socialaccount django allauth would redirect you to /accounts/profile/. But we get an error as we do not have that template and url for that template setup yet.
  3. So, First of all let’s map the /accounts/profile/ to a template (in my case i am going to name it “profile.html”)
  4. Open up the sampleapp/ and add the following code to it.
  5. Next thing that we do is to create the template, So create a profile.html in the templates directory and add the following code to it.
  6. Okay, that needs some explanation! Let’s go step by step.
    • Line 1 : here we simply extend our template with the base template.
    • Line 3 – 4 : We Opened the blocks body and content as this is where we’ll append all the user data.
    • Line 6: Here, we check if there exists a user object.
    • Line 7: Please Note, Here {{ user.first_name }} is being extracted from the User model and not SocialAccount Model.
    • Line 9: As one user may have multiple social accounts linked to the user model, here we iterate the Related Manager object (socialaccount_set) with a for loop. Here we are checking for each social account that is linked to the user model & for each social account –> We display the data from Lines 10 – 16
  7. You can simply use a if tag, if you are looking for a specific provider, for example:
  8. Alternatively, you can also import the user object to views and filter the information on the view level and pass only required information to the template [which is how it should be done].

Hope this helps 🙂