How to lookup django session for a particular user?

后端 未结 6 1516
太阳男子
太阳男子 2020-12-07 11:13

I am writing an application where I will be accessing the database from django and from a stand alone application. Both need to do session verification and the session shoul

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 11:44

    I found this code snippet

    from django.contrib.sessions.models import Session
    from django.contrib.auth.models import User
    
    session_key = '8cae76c505f15432b48c8292a7dd0e54'
    
    session = Session.objects.get(session_key=session_key)
    uid = session.get_decoded().get('_auth_user_id')
    user = User.objects.get(pk=uid)
    
    print user.username, user.get_full_name(), user.email
    

    here http://scottbarnham.com/blog/2008/12/04/get-user-from-session-key-in-django/

    Have not verified it yet, but looks pretty straight forward.

提交回复
热议问题