烧瓶形式。错误的请求

Flask forms. Bad Request

我的 html 中有两个表格。第一个工作正常。第二个 returns Bad Request The browser (or proxy) sent a request that this server couldn't understand html:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <form method="post" >
        <input type="text" name="ssid">
        <input type="password" name="password">
        <input type="submit" value="Connect">
        </form>
        <form method="post" >
        <input type="hidden" name="ind" value="1">
        <input type="submit"  value="Disconnect">
        </form>
    </body>
</html>

Python:

@app.route('/', methods=['POST'])
@app.route('/index', methods=['POST'])
def wifi_form():
    wifi_ssid=request.form['ssid']
    wifi_pass=request.form['password']
    result='Connect'
    return result
@app.route('/', methods=['POST'])
@app.route('/index', methods=['POST'])
def del_form():
    ind=request.form['ind']
    result='disconnect'
    return result

我认为你采取了错误的方法。启动本地服务器时会弹出索引页面,而且您期望结果的形式只会在用户输入值后出现,您的路线都搞砸了。请参阅此处的文档.. https://scotch.io/tutorials/getting-started-with-flask-a-python-microframework

我正在为您重新格式化代码..希望这对您有所帮助

main.py

from flask import Flask, render_template, request


app = Flask(__name__)

@app.route('/')
def index():
    return render_template('main.html')

@app.route('/wifi_form', methods=['POST'])
def wifi_form():
    wifi_ssid=request.form['ssid']
    wifi_pass=request.form['password']
    result='Connect'
    return render_template('main.html', result=result)

@app.route('/del_form', methods=['POST'])
def del_form():
    ind=request.form['ind']
    result='disconnect'
    return render_template('main.html', result=result)

将以下 html 代码放在同一目录下的模板文件夹中

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <form action="{{url_for('wifi_form')}}" method="post" >
        <input type="text" name="ssid">
        <input type="password" name="password">
        <input type="submit" value="Connect">
        </form>
        <form action="{{url_for('del_form')}}" method="post" >
        <input type="hidden" name="ind" value="1">
        <input type="submit"  value="Disconnect">
        </form>

      {% if result %}
           {{ result }}
     {% endif %}    

    </body>
</html>