【问题标题】:pressing button on html, Flask doesn't do anything [duplicate]按下html上的按钮,Flask没有做任何事情[重复]
【发布时间】:2017-04-24 21:16:32
【问题描述】:

我有一个只有一个按钮的网站。按下按钮时,我想使用 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)

知道为什么什么都没发生吗?为什么当我按下按钮时,我没有收到“是的,它可以工作”的消息?

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>

【问题讨论】:

    标签: python html flask request vps


    【解决方案1】:

    两件事

    1. 你的 python 中有一条重复的路由

    你可能想这样做

    @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 将您的按钮包装在 &lt;form&gt; 元素中

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2017-12-31
      • 2019-07-13
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多