本文介绍了werkzeug.routing.BuildError:无法为端点“成功"构建URL.您是否忘记了指定值['name']?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决此错误?我已经尝试了很多事情,但我自己无法解决.

How to solve this error? I have tried many things but can't solve it by myself.

http_methods.py:

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']的值;如何解决这个错误?

I get an error in the login method, that value of ['name'] is not specified; how to solve this error?

login.html:

login.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版本.

I am using python 3.7 and flask 1.0.2 version right now.

推荐答案

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

Have a look at this. In your login function I added a render_template for the first GET request when opening the login page. This will probably also prevent the error you're getting now.Bear in mind this does not contain any error handling, like what happens if user is unknown in your login procedure.

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

这篇关于werkzeug.routing.BuildError:无法为端点“成功"构建URL.您是否忘记了指定值['name']?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 19:35