我在 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
在这里我喜欢用它作为装饰器?但不明白怎么做,即为用户键返回部分上面
的函数我喜欢用作装饰器。
# 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
我知道我们可以使用装饰器来引发错误,但如果成功,我也想返回一些值。我的问题是我们可以写装饰器吗?或者我们只需要在函数调用中使用返回