【问题标题】:Flask - redirect url_for ErrorFlask - 重定向 url_for 错误
【发布时间】:2013-03-14 07:46:00
【问题描述】:

以下代码在我的烧瓶应用程序中出现错误。

@@app.route('/')
.....

return redirect(url_for('nextPage'),id=DBTable.id)


@app.route('/<path:id>')
@login_required
def nextPage(id):
return render_template('page2.html')               

Error - 
---------------------------------------------------------------------------
File "C:\Python27\lib\site-packages\werkzeug\routing.py", line 1607, in build
raise BuildError(endpoint, values, method)
BuildError: ('nextPage', {}, None)
<SocketIOServer fileno=116 address=0.0.0.0:5000>: Failed to handle request:
request = POST /landingPage HTTP/1.1 from ('127.0.0.1', 50287)
application = <flask.app.Flask object at 0x0000000002643B70>

请帮我解决上述问题

【问题讨论】:

  • 尝试在路由末尾添加'/'作为@app.route('//')

标签: python flask-wtforms flask-extensions flask


【解决方案1】:

除了所有明显的语法错误之外,应该通过将传递给路由的参数放在 url_for 块中来解决问题。

@app.route('/')
def index():
    # ...
    return redirect(url_for('nextPage', id=DBTable.id))

@app.route('/<id>')
def nextPage(id):
    # ...
    return render_template('page2.html')

【讨论】:

  • 感谢 Teisman 的回复,但更改后仍然看到同样的错误。
  • 很遗憾听到这个消息。你确定这是同一个错误吗?我问这个是因为它可以在我的机器上运行。调试时使用更简单的代码永远不会有坏处。由于我们暂时没有使用id,所以尝试填写一个虚拟值,例如'test',使其变为return redirect(url_for('nextPage', id="test")),并检查是否再次抛出相同的错误。
【解决方案2】:

尝试使用此代码

from flask import *

app = Flask(__name__)

@app.route('/')
def index():
    return redirect(url_for('random', id="blah blah"))

@app.route('/<id>')
def random(id):
    return id

if __name__ == '__main__':
    app.run(debug=True)

如果您有任何错误,请告诉我。

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 2014-04-14
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    相关资源
    最近更新 更多