Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : show the username to the user when logged in

Tags:

django

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'})
like image 847
Hamid Hoseini Avatar asked Dec 02 '22 19:12

Hamid Hoseini


1 Answers

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

like image 52
karthikr Avatar answered Apr 12 '23 02:04

karthikr