0

I have created a small project using python social auth for logging with facebook.I have referred python social auth example at link.I can able to get the user name with request.name.But how can i get the other data like first_name,last_name,email.Any help would be appriciated

example code in views is:

def home(request):
   context = RequestContext(request,
                       {'request': request,
                        'user': request.user})
   return render_to_response('home.html',
                         context_instance=context)
Mulagala
  • 8,231
  • 11
  • 29
  • 48

1 Answers1

1

Here is one way: request.user has a helper method called is_authenticated, which you can use like this:

ctx = {
       'user' : request.user
      }

if request.user.is_authenticated():
    ctx.update({
         'first_name': request.user.first_name,
         'last_name': request.user.last_name,
         'email' : request.user.email
    })

Now, the extra attributes are present only if the user is authenticated This is, assuming you are using the auth model of django. If not, please change the model field names accordingly.

On a side note, you should not be passing the entire request object in the context. You should be extracting only the relevant info and sending it in the context.

Now, you can access all these values from the template anyways. Example:

{% if request.user.is_authenticated %}
    {{request.user.username}}
    {{request.user.email}}
    {# etc.. #}
{% endif %}

No need to send anything in the context explicitly. The request object already has everything

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • Thank you and what are the list of values we are getting if user is authenticated...? – Mulagala Oct 20 '14 at 18:51
  • 1
    You have to add `SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']` in `settings.py` in order to obtain the email from Facebook. – aldo_vw Oct 20 '14 at 18:52
  • 1
    If you mean _not_ authenticated, this context actually does not make any sense,as it would have `user`. On second thought, you can get all these values from the template itself. Let me edit the answer – karthikr Oct 20 '14 at 18:54