我的html中有两种形式。第一个正常工作。第二个返回Bad Request浏览器(或代理)发送了该服务器无法理解的请求 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
答案 0 :(得分:1)
我认为您的做法不正确。当您启动本地服务器时,将弹出“索引”页面,并且只有在用户输入值之后,您的路由才会被弄乱,您期望的结果形式也只会出现。请在此处参考文档。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代码放在同一目录的templates文件夹中
<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>