Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BadRequestKeyError_Python_Flask - Fatal编程技术网

Python BadRequestKeyError

Python BadRequestKeyError,python,flask,Python,Flask,已经提出了多个问题,但都出现了类似的错误。我已经尝试了所有的解决方案,但仍然得到一个漂亮的错误: werkzeug.exceptions.HTTPException.wrap..newcls:400错误请求:KeyError:'username' 以下是我的html表单: <form action='/login' method = "GET"> <label>Name: </label> <input name="username" t

已经提出了多个问题,但都出现了类似的错误。我已经尝试了所有的解决方案,但仍然得到一个漂亮的错误:

werkzeug.exceptions.HTTPException.wrap..newcls:400错误请求:KeyError:'username'

以下是我的html表单:

<form action='/login' method = "GET">
    <label>Name: </label>
    <input name="username" type="text">
    <input type="submit" name='submit' value='submit'>
</form>
我已经学会了瓶子和向烧瓶过渡。 到目前为止,我已经尝试了以下方法: 我去邮局 2请求。表单。获取“用户名”,无 3在功能路线中添加帖子原因,没有任何押韵或原因


有人能帮我吗

您需要更改html文件中的GET to POST方法,并将该方法添加到decorator@app.route中。不要忘记,在这种情况下,呈现html文件需要返回GET

@app.route("/login", methods = ['GET', 'POST'])
def login():
if request.method == "POST":
    # import pdb; pdb.set_trace()
    un = request.form.get('username', None)
    return un

return render_template('form.html')
提示:了解pdb可以更轻松地调试程序


无需更改窗体操作方法。但当您使用GET方法发送表单数据时,您的数据将作为URL变量传输,您需要以这种方式读取数据:

if flask.request.method == 'GET':
    username = flask.request.args.get('username')
如果您将方法更改为POST,请阅读以下内容:

if flask.request.method == 'POST':
    username = flask.request.values.get('username')

我按照您的指示得到了以下消息:AttributeError:“Request”对象没有属性“get”。它是Request.form.get。谢谢为什么不让方法工作呢?
if flask.request.method == 'POST':
    username = flask.request.values.get('username')