Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user object using userid in django

Tags:

python

django

Hello i am new to django,

i am creating an authentication system using django.

Once a user is logged in i am storing the value in a session.

 user = authenticate(username=username, password=password)
request.session['mid'] = user.id

and when i refresh i can receive the session id

uid = request.session['mid']

But i am not sure how to get the userdatas from the user id. can any one tell me how can get the user object using the user id.

like image 989
Ramesh Avatar asked Aug 31 '25 01:08

Ramesh


1 Answers

Use simple .get() query.

try:
    uid = request.session['mid']
    userobj = User.objects.get(id=uid)
except User.DoesNotExist:
   #handle case when user with that id does not exist

...
like image 174
Rohan Avatar answered Sep 02 '25 15:09

Rohan