【问题标题】:Most simple button doesn't work最简单的按钮不起作用
【发布时间】:2017-06-03 15:16:22
【问题描述】:

我有一个非常简单的 HTML 提交按钮,但它不起作用。

ma​​in_page.html

<form class="main_page" method="POST" action=".">
    <div class="form-row">
         <input type="submit" name="invoer" value="Invoeren"/>
    </div>
</form>

flask_app.py

from flask import Flask, render_template, request

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/", methods=["GET", "POST"])

def main():
    if request.method == "GET":
        return render_template("main_page.html")

    if request.form["invoer"] == "POST":
        return render_template("main_page.html")

当我点击按钮时,它会显示:

----------------------------------- -------------------------------------------------- ----------------------------------


我之前已经制作了一个这样的按钮,但后来它起作用了,也许我做了一些不同的事情。我该怎么办?

【问题讨论】:

  • 1.你也看过日志吗? 2. 那真的是你的 HTML 吗?明显坏了。
  • 1.是的,我已经这样做了,但它给了我这个pastebin.com/pZE0KY3E,我不知道为什么以及这意味着什么。 2. HTML只是完整html文件的一部分,我添加了复制时错过的''
  • 在问题中输入minimal reproducible example
  • 都在那里。

标签: python html flask pythonanywhere


【解决方案1】:

错误显示ValueError: View function did not return a response,这意味着当您单击 HTML 中的提交按钮时,您的 POST 没有返回任何对模板的响应。将您的代码flask_app.py 更改为:

from flask import Flask, render_template, request

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/", methods=["GET", "POST"])

def main():
    if request.method == "GET":
        return render_template("main_page.html")

    if request.method == "POST": # change code here
        return render_template("main_page.html")

这将使它工作,但是,它将返回相同的模板。

【讨论】:

  • 这就是我所需要的!谢谢:)
猜你喜欢
  • 2023-03-04
  • 2021-04-30
  • 1970-01-01
  • 2017-12-15
  • 2015-02-06
  • 1970-01-01
  • 2015-04-01
  • 2018-01-17
  • 2012-10-28
相关资源
最近更新 更多