简体   繁体   English

400错误请求python烧瓶

[英]400 Bad Request python flask

Hi I'm new to python flask, i have what seems a simple issue of a 400 bad request error, that should be due to having wrongly named variables in form when trying to pass them to a function in python flask.嗨,我是 python flask 的新手,我有一个看起来很简单的 400 错误请求错误问题,这应该是由于在尝试将它们传递给 python flask 中的函数时,表单中的变量命名错误。 I have done some research but i still can't figure out where i go wrong with this code, any help would be really appreciated.我已经做了一些研究,但我仍然无法弄清楚这段代码哪里出了问题,任何帮助都将不胜感激。 Here is the code for the html form这是html表单的代码

<html>
<body>

  <h1>Add a Munroe to your list</h1>
    <form action = "{{ url_for('addmunro') }}" method="POST"
      enctype = "multipart/form-data">

      Name<br>
      <input type="text" name="mnName"/><br>
      Description<br>
      <input type="text" name="mnDesc"/><br>
      Region<br>
      <input type="text" name="mnRegion"/><br>
      Height<br>
      <input type="text" name="mnHeight"/><br>
      Walk date<br>
      <input type="text" name="mnDate"/><br>
      Walk image<br>
      <input type="text" name="mnImage"/><br>

      <br>
      <br>
      <input type="submit" name="add-munro.html" value = "ADD MUNRO"/>

    </form>
</body>
</html>

And here is the code for the python flask application这是python烧瓶应用程序的代码

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

@app.route('/add-munro.html', methods=['GET'])
def addmunro():


    #Create an empty list
    mnList={}

    #Create a munro dictionary
    munro = {'name':request.form['mnName'],
             'desc':request.form['mnDesc'],
             'region':request.form['mnRegion'],
             'height':request.form['mnHeight'],
             'date':request.form['mnDate'],
             'image':request.form['mnImage']}

    #the munro dictionary is added to mnList
    #mnList.append(munro)

    return render_template('add-munro.html')


if __name__ == "__main__":
  app.run(host = '0.0.0.0', debug = True)

You are making a POST request in your form, but only allow the GET method in your app route.您正在表单中发出POST请求,但只允许在您的应用程序路由中使用GET方法。 Change @app.route('/add-munro.html', methods=['GET']) to @app.route('/add-munro.html', methods=['POST']) .@app.route('/add-munro.html', methods=['GET'])更改为@app.route('/add-munro.html', methods=['POST'])

There are several mistakes:有几个错误:

  1. You are making POST request but handling only GET request您正在发出 POST 请求但仅处理 GET 请求
  2. .html is not needed in routing路由中不需要 .html
  3. munro object is not passed to the template munro 对象未传递给模板

I have updated these and now it's good to go:我已经更新了这些,现在可以开始了:

application.py

from flask import Flask, render_template, request, url_for

app = Flask(__name__)    

@app.route('/add-munro', methods=['GET','POST'])
def addmunro():
    if request.method == "POST":
        #Create an empty list
        mnList={}
        #Create a munro dictionary
        munro = {'name':request.form['mnName'],
                 'desc':request.form['mnDesc'],
                 'region':request.form['mnRegion'],
                 'height':request.form['mnHeight'],
                 'date':request.form['mnDate'],
                 'image':request.form['mnImage']}
        return render_template('add-munro.html', munro=munro)
    else:
        return render_template('add-munro.html')

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

add-munro.html

<html>
<body>
    {% if munro is defined -%}
    <h3>
        Name: {{ munro.name }}
    </h3>
    <h3>
        Description: {{ munro.desc }}
    </h3>
    <h3>
        Region: {{ munro.region }}
    </h3>
    <h3>
        Height: {{ munro.height }}
    </h3>
    {%- endif %}
    <h1>Add a Munroe to your list</h1>
    <form action = "{{ url_for('addmunro') }}" method="POST"
    enctype = "multipart/form-data">

    Name<br>
    <input type="text" name="mnName"/><br>
    Description<br>
    <input type="text" name="mnDesc"/><br>
    Region<br>
    <input type="text" name="mnRegion"/><br>
    Height<br>
    <input type="text" name="mnHeight"/><br>
    Walk date<br>
    <input type="text" name="mnDate"/><br>
    Walk image<br>
    <input type="text" name="mnImage"/><br>

    <br>
    <br>
    <input type="submit" value = "ADD MUNRO"/>
</form>
</body>
</html>

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM