在 html 上按下按钮,Flask 不执行任何操作

pressing button on html, Flask doesn't do anything

我有一个一键式网站。按下按钮时,我想使用 Flask 发送消息。

from flask import Flask
from flask import render_template
from flask import request


app = Flask(__name__)
@app.route("/")
def index():
    return render_template('index.html')

@app.route("/")
def login():
    if request.method == 'POST':
        return 'yes it works'

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

知道为什么没有任何反应吗?为什么当我按下按钮时,我没有收到消息 "yes it works"?

html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<meta name="generator" content="WYSIWYG Web Builder 11 - http://www.wysiwygwebbuilder.com">
<link href="{{ url_for('static', filename='css/Untitled3.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='css/index.css') }}" rel="stylesheet">
</head>
<body>
<input type="submit" id="Button1" name="" value="Submit" style="position:absolute;left:382px;top:298px;width:96px;height:25px;z-index:0;">
</body>
</html>

两件事

  1. 您的 python
  2. 中有重复的路线

你可能想做

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

@app.route("/login", methods=['POST'])
def login():
  if request.method == 'POST':
    return 'yes it works'

而您的 html 将您的按钮包裹在 <form> 元素中

<body>
<form action="/login" method="post">
<input type="submit" id="Button1" name="" value="Submit" style="position:absolute;left:382px;top:298px;width:96px;height:25px;z-index:0;">
</form>
</body>