gpt4 book ai didi

python - werkzeug.routing.BuildError : Could not build url for endpoint 'success' . 您是否忘记指定值 ['name' ]?

转载 作者:太空宇宙 更新时间:2023-11-04 02:07:11 24 4
gpt4 key购买 nike

"werkzeug.routing.BuildError: Could not build url for endpoint 'success'. Did you forget to specify values ['name']?"

如何解决这个错误?我尝试了很多事情,但无法自己解决。

http_methods.py:

from flask import Flask, redirect, url_for, request
app = Flask(__name__)

@app.route('/success/<name>')
def success(name):
return 'welcome %s' % name

@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['nm']
return redirect(url_for('success', name = user))
else:
user = request.args.get('nm')
return redirect(url_for('success', name = user))

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

我在登录方法中遇到错误,未指定 ['name'] 的值;如何解决这个错误?

登录.html:

<!DOCTYPE html>
<html lang="en">
<body>
<form action="http://127.0.0.1:5000/login" method="POST">
<p>Enter name:</p>
<p><input type="text" name="nm" value="nm"/></p>
<p><input type="submit" value="submit"/></p>
</form>
</body>
</html>

输出:

  File "/Users/chirag.kanzariya/pythonprojects/v_python/lib/python3.7/site-packages/flask/helpers.py", line 345, in url_for
force_external=external)
File "/Users/chirag.kanzariya/pythonprojects/v_python/lib/python3.7/site-packages/werkzeug/routing.py", line 1776, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'success'. Did you forget to specify values ['name']?
127.0.0.1 - - [29/Jan/2019 14:48:00] "GET /login?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [29/Jan/2019 14:48:00] "GET /login?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [29/Jan/2019 14:48:00] "GET /login?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [29/Jan/2019 14:48:00] "GET /login?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [29/Jan/2019 14:48:00] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [29/Jan/2019 14:48:00] "GET /login?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

我现在使用的是 python 3.7 和 flask 1.0.2 版本。

最佳答案

看看这个。在您的登录功能中,我在打开登录页面时为第一个 GET 请求添加了一个 render_template。这也可能会阻止你现在得到的错误。请记住,这不包含任何错误处理,例如如果用户在您的登录过程中未知会发生什么。

@app.route('/success/<name>')
def success(name):
return 'welcome %s' % name

@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['nm']
return redirect(url_for('success', name = user))

return render_template(login.html)

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

关于python - werkzeug.routing.BuildError : Could not build url for endpoint 'success' . 您是否忘记指定值 ['name' ]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54417742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com