温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Flask error: werkzeug.routing.BuildError

发布时间:2020-07-19 19:20:55 来源:网络 阅读:1672 作者:DevOperater 栏目:开发技术


@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')


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI