Madison Leopold
Madison Leopold

Reputation: 415

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.

Upvotes: 1

Views: 3897

Answers (1)

TOTO
TOTO

Reputation: 307

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.

Upvotes: 1

Related Questions