I have the following 4 lines of code that I keep reusing in my django views. I would like a function that passes the 开发者_如何学运维final value (All the objects of a logged in user) to my other functions and how to call that. As a fix, I have to keep using this lines in all my functions.
sessionkey = request.session.session_key
session = Session.objects.get(session_key=sessionkey)
uid = session.get_decoded().get('_auth_user_id')
user = UserProfile.objects.get(pk=uid)
Thanks
def get_userprofile_from_session_via_request(request):
sessionkey = request.session.session_key
session = Session.objects.get(session_key=sessionkey)
uid = session.get_decoded().get('_auth_user_id')
user = UserProfile.objects.get(pk=uid)
return user
Of course, I'm not sure why you wouldn't simplify this to:
def get_userprofile_from_session_via_request(request):
user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
return user
精彩评论