簡體   English   中英

POST 方法不適用於 Flask 應用程序

[英]POST method not working on Flask application

我正在嘗試基於多個簡單教程運行一個簡單的 Flask 應用程序。 我的目標是完成一個完整的教程並操作代碼來構建一個搜索 web 應用程序,該應用程序連接到 SQL 服務器數據庫。

但是,當我嘗試運行代碼並提供 integer 輸入時,POST 方法不起作用,因此它不會返回指定的{{value}}

我嘗試添加和刪除幾個組件:在 HTML 上添加action='/' ,並嘗試在沒有它的情況下運行代碼。 這並沒有什么不同。

我已經有了methods=['GET', 'POST']

我什至嘗試過{{ 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>

在運行代碼時,我的應用程序成功呈現,但在提交數字時,服務器沒有注冊輸入。

我什至沒有收到錯誤。 我在終端中看到的只是"GET / HTTP/1.1" 200

通過刪除onsubmit="return false;" HTML中表單的屬性。 並通過重新啟動您的應用程序並重新加載HTML 它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM