简体   繁体   English

POST 方法不适用于 Flask 应用程序

[英]POST method not working on Flask application

I'm trying to run a simple Flask application based on multiple simple tutorials.我正在尝试基于多个简单教程运行一个简单的 Flask 应用程序。 My goal is to finish a full tutorial and manipulate the code to build a search web app that connects to a SQL server database.我的目标是完成一个完整的教程并操作代码来构建一个搜索 web 应用程序,该应用程序连接到 SQL 服务器数据库。

However, when I try running the code and provide an integer input, the POST method is not working, so it will not return a {{value}} as specified.但是,当我尝试运行代码并提供 integer 输入时,POST 方法不起作用,因此它不会返回指定的{{value}}

I've tried adding and removing several components: adding in the action='/' on the HTML, and trying to run the code as well without it.我尝试添加和删除几个组件:在 HTML 上添加action='/' ,并尝试在没有它的情况下运行代码。 That didn't make a difference.这并没有什么不同。

I have methods=['GET', 'POST'] already.我已经有了methods=['GET', 'POST']

I even tried {{ url_for('/') }} and that just gave me an Internal Server Error.我什至尝试过{{ url_for('/') }}但这只是给了我一个内部服务器错误。

from flask_sqlalchemy import SQLAlchemy

    app = Flask(__name__)

    @app.route('/hello')
    def hello():
        return "My flask app"

    @app.route('/', methods=["GET", "POST"])
    def home():
        if request.method == "POST":
            value = int(request.form["number"])
            add_one = value + 1
            return render_template('index.html', string="WORLD!", value=add_one)
        return render_template('index.html', string="WORLD!")


    if __name__ == "__main__":
        app.run()
    HTML:

    <body>
        <div class="container">
          <h1>Hello, {{string}}</h1>
          <br>
            <form action='/' role="form" method="post" onsubmit="return false;">
              <div class="form-group">
                  <input type="number" class="form-control" name="number" placeholder="Enter a number" required>
              </div>
              <button type="submit" class="btn btn-default">Submit</button>
          </form>
          <p>The calculated number is {{value}}</p>
        <br>
        </div>
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
      </body>

While running the code my app renders successfully, but when submitting a number the server does not register the input.在运行代码时,我的应用程序成功呈现,但在提交数字时,服务器没有注册输入。

I don't even receive an error.我什至没有收到错误。 All I see in the terminal is "GET / HTTP/1.1" 200 .我在终端中看到的只是"GET / HTTP/1.1" 200

By removing onsubmit="return false;"通过删除onsubmit="return false;" attribute of the form in the HTML . HTML中表单的属性。 And by restarting your app and reload the HTML .并通过重新启动您的应用程序并重新加载HTML It should be working.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM