I need to add a python decorator to Flask route functions, (basically I edited the code from here)
def requires_admin(f):
def wrapper(f):
@wraps(f)
def wrapped(*args, **kwargs):
#if not admin:
#return render_template('error.html')
return f(*args, **kwargs)
return wrapped
return wrapper
and use it like this will be OK:
@app.route('/admin/action')
@requires_admin
def AdminAction():
#NO error if NO parameter
But use it like this will have error:
@app.route('/admin/action/<int:id>')
@requires_admin
def AdminAction(id):
In Flask 0.10, I get errors like this (I just updated from Flask 0.9 to 0.10, and in Flask 0.9 there is no grammar error like this):
@requires_admin
File "/usr/local/lib/python2.6/dist-packages/Flask-0.10.1-py2.6.egg/flask/app.
py", line 1013, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File "/usr/local/lib/python2.6/dist-packages/Flask-0.10.1-py2.6.egg/flask/app.
py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/Flask-0.10.1-py2.6.egg/flask/app.
py", line 984, in add_url_rule
'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint functi
on: wrapper
I am pretty new to the decorator stuff, how do I correct this error?

所有评论(0)