[ Django: User object's 'email' attribute ]
I'm working on an application where users can log in into my application using their Twitter account credentials which I implemented using the python-social-auth
. Now, Django's authentication system provides User
objects, with five primary attributes of username
, password
, email
, first_name
and last_name
as given in the documentation: https://docs.djangoproject.com/en/1.7/topics/auth/default/#user-objects
Now, after the user successfully logs in using his/her Twitter account, I want certain fields of my form, like the user's full name, email address, Twitter account username, etc to be pre-filled. For that purpose, I used something like this for pre-filling user's full name form field:
{% if user and not user.is_anonymous %}
Full Name: <input type = "text" name = "name" value = "{{user.get_full_name}}"">
{% endif %}
This is what the above form field looks like after a user successfully logs in through his/her Twitter account:
Perfect! Works just as intended. Similarly, attributes such as username
, last_name
, first_name
works just fine for the currently logged in User
object.
However, when I try to pre-fill the email address field in my form using:
Email Address: <input type = "text" name = "email" value = "{{user.email}}"">
However, the value of {{user.email}}
doesn't return anything, and my form field looks something like this:
Why is the email
attribute of the User
object not returning the email address through which the user logged in using his Twitter credentials? All other User
attributes seem to be working just fine. Any reasoning behind this?
EDIT 1:
This is what my database at the admin looks like:
So the email
field is empty, hence the User
object returns nothing for that attribute value. But why is the email address not stored in the database every time a new user logs in using their Twitter accounts?
Answer 1
The email is stored elsewhere when using python-social-auth. They create a SocialUser table and likely the data you seek is stored in JSON format in the extra_data
field - look at the model created by PSA
Pulling the additional info may not be as trivial as before, but you can add functionality that will save the email address to your user's table when the social user is created. See Pipeline
Answer 2
In admin if user_email is not showing probably there is no email added with perticular user.
Just make sure this by checking database again.
Answer 3
It's happening probably because you have nothing in your register process that actually stores the email in the database for the form, so in your view have something like
p = #user object from form.
p.email = request.POST['email']
p.save()