5

How can I show any user his/her username when logged in at "next.html" page ???

def signin(request):
    if request.method != 'POST':
        return render(request, 'login.html', {'error': 'Email or Password is incorrect'})

    user_profile = User_account.objects.filter(e_mail = request.POST['user_email'],     passwd = request.POST['password'])     
    if user_profile:
        return render(request, 'next.html', {'name' :User_account.objects.**WHAT SHOULD I WRITE HERE???** } )
    else:
        return render(request, 'login.html', {'error': 'Email or Password is incorrect'})
Hamid Hoseini
  • 123
  • 2
  • 3
  • 7

1 Answers1

21

There are 2 ways you can do it:

a) Directly in the template:

{{request.user.username}}

Or: b)

return render(request, 'next.html', {'name' : request.user.username })

Note that you have not "logged" the user in yet

In your case, you can do:

return render(request, 'next.html', {'name' : user_account.first_name })

You need to call the login method to actually log the user in, or manually handle the login. This post can give you the way to do it.

Also you would need a request_context processor

Community
  • 1
  • 1
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • 1
    He's also gonna need request context processor enabled. – Jan Matějka Jun 09 '13 at 13:36
  • I wrote return render(request, 'next.html', {'name' : user_account.username }) but it doesn't work – Hamid Hoseini Jun 09 '13 at 13:54
  • how is you User_account model defined? – karthikr Jun 09 '13 at 13:54
  • from django.db import models class User_account(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=20) e_mail = models.EmailField() passwd = models.CharField(max_length=16) def __unicode__(self): return self.first_name – Hamid Hoseini Jun 09 '13 at 13:56
  • you dont have a username field. What do you want to show as username ? – karthikr Jun 09 '13 at 14:09
  • In my opinion, you should inherit from `django.contrib.auth.models.User` Read up about it in the django documentation – karthikr Jun 09 '13 at 14:10
  • 1
    I still believe you got the whole models wrong. Read the documentation again. The django contrib auth models does what you are looking to do, and a lot more – karthikr Jun 09 '13 at 14:26
  • {{request.user.username}} worked flawlessly. Thank you! – Shilpa Mar 27 '16 at 11:09