我们可以从python decoretor返回变量吗?

时间:2015-06-23 10:35:57

标签: python-2.7 flask python-decorators django-sessions django-1.8

我在django中使用了检查会话密钥的功能(需要编码)。

def sessioncheck(unikey):
   from django.contrib.sessions.models import Session
   try:
       session_obj = Session.objects.get(pk=unikey)
       userinfo = session_obj.get_decoded()
       # Here i want some data to return 
       userkey = userinfo["session_datakey"] 
       username = userinfo["session_user"] 
       return userkey,username # i need it in my function and as many other stuff to with it
   except(ObjectDoesNotExist, KeyError):
       response = JsonResponse({"expire": "u r not here anymore"})
       return response

在这里,我喜欢将它用作装饰器?但没有得到它怎么办,即返回用户密钥的部分 上面的函数我喜欢用作decorector。

# Currunt i m using as
def sessioncall(request, unikey):
      gotukey,username = sessioncheck(unikey)
      if type(token).__name__ is not "JsonResponse":
           # Lots of work using **gotukey** and **username** over here 
           # work change from function to function for use gotukey
           response = JsonResponse({"hmm": "man you are in now"})
           return response
      else:
           return gotukey

我知道我们可以使用 decorator 来引发错误,但如果成功,我还想返回一些值。
我的问题是我们可以写装饰器吗?或者我们只需要在函数调用中使用返回

0 个答案:

没有答案