如何将index()
传递给称为uppercase()
的装饰器函数。我希望从index()
传递的值大写。
我收到此错误:
500 Internal Server Error
The server encountered an unexpected condition which prevented it from fulfilling the request.
Traceback (most recent call last):
File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 638, in respond
self._do_respond(path_info)
File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 701, in _do_respond
self.hooks.run('before_finalize')
File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 95, in run
self.run_hooks(iter(sorted(self[point])))
File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 117, in run_hooks
hook()
File "/home/user/.local/lib/python3.6/site-packages/cherrypy/_cprequest.py", line 65, in __call__
return self.callback(**self.kwargs)
TypeError: uppercase() missing 1 required positional argument: 'func'
import cherrypy
from cherrypy import tools
@cherrypy.tools.register('before_finalize')
def uppercase(func):
def wrapper():
original_result = func()
modified_result = original_result.upper()
return modified_result
return wrapper
class HelloWorld(object):
@cherrypy.expose
@cherrypy.tools.uppercase()
def index(self):
return 'Hello!'
if __name__ == '__main__':
cherrypy.tools.uppercase = cherrypy.Tool('before_finalize', uppercase)
cherrypy.quickstart(HelloWorld())
您应该这样使用装饰器:
@cherrypy.tools.uppercase
def index(self):
return 'Hello!'
注意()
之后缺少uppercase
。