简体   繁体   中英

POST method not working on Flask application

I'm trying to run a simple Flask application based on multiple simple tutorials. 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.

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.

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. That didn't make a difference.

I have methods=['GET', 'POST'] already.

I even tried {{ url_for('/') }} and that just gave me an Internal Server Error.

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 .

By removing onsubmit="return false;" attribute of the form in the HTML . And by restarting your app and reload the HTML . It should be working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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