我在我的函数中检索了用户,所以我把它拉到装饰器中,问题是,python告诉我用户没有定义,但我认为装饰的函数能够在装饰器中使用变量范围没有作为参数传递。
def authenticator(f):
@wraps(f)
def decorator(request, *args, **kwargs):
body = json.loads(request.body)
usern = body["username"]
passw = body["password"]
from django.contrib.auth import authenticate
user = authenticate(username = usern, password = passw)
return f(request, *args, **kwargs)
return decorator
这里是我抛出的堆栈跟踪
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/http.py" in inner
45. return func(request, *args, **kwargs)
File "/Users/krishna/Documents/irrigationwebserver/webserver/irrigationservice/utils.py" in decorator
24. return f(request, *args, **kwargs)
File "/Users/krishna/Documents/irrigationwebserver/webserver/irrigationservice/views.py" in checkMoistureProfile
92. if user is not None:
Exception Type: NameError at /checkMoistureProfile/
Exception Value: global name 'user' is not defined