开发者

Django function that passes variables to other functions

开发者 https://www.devze.com 2022-12-22 16:47 出处:网络
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 func

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
0

精彩评论

暂无评论...
验证码 换一张
取 消