首页 > 代码库 > Flask error: werkzeug.routing.BuildError
Flask error: werkzeug.routing.BuildError
@main.route(‘/sendfile‘, methods=[‘GET‘, ‘POST‘]) def sendfile(): if request.method == ‘POST‘: f = request.files[‘file‘] basepath = path.abspath(path.dirname(__file__)) upload_path = path.join(basepath, ‘static/uploads‘) f.save(upload_path + ‘/‘ + secure_filename(f.filename)) return redirect(url_for(‘sendfile‘)) return render_template(‘sendfile.html‘)
^CwulilideMacBook-Pro:Manage root# python Manage.py runserver
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
127.0.0.1 - - [21/Nov/2016 18:24:31] "GET /sendfile HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:32] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:32] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:34] "GET /sendfile HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:40] "POST /sendfile HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/wulili/myproject/Manage/app/main/views.py", line 25, in sendfile
return redirect(url_for(‘sendfile‘))
File "/Library/Python/2.7/site-packages/flask/helpers.py", line 312, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/helpers.py", line 305, in url_for
force_external=external)
File "/Library/Python/2.7/site-packages/werkzeug/routing.py", line 1678, in build
raise BuildError(endpoint, values, method)
BuildError: (‘sendfile‘, {}, None)
127.0.0.1 - - [21/Nov/2016 18:24:40] "GET /sendfile?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:40] "GET /sendfile?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:40] "GET /sendfile?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:40] "GET /sendfile?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:40] "GET /sendfile?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:40] "GET /sendfile?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2016 18:24:40] "GET /sendfile?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
* Detected change in ‘/Users/wulili/myproject/Manage/app/main/views.py‘, reloading
* Restarting with stat
解决方法:
if you are using blueprints, url_for should be invoked as url_for(blueprint_name.func_name).
main 为蓝图的名字
修改后的代码为
@main.route(‘/sendfile‘, methods=[‘GET‘, ‘POST‘])
def sendfile():
if request.method == ‘POST‘:
f = request.files[‘file‘]
basepath = path.abspath(path.dirname(__file__))
upload_path = path.join(basepath, ‘static/uploads‘)
f.save(upload_path + ‘/‘ + secure_filename(f.filename))
return redirect(url_for(‘main.sendfile‘))
return render_template(‘sendfile.html‘)
Flask error: werkzeug.routing.BuildError