Search code examples
pythonhtmlflaskpythonanywhere

Most simple button doesn't work


I have a really easy submit button in HTML, but it doesn't work.

main_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")

When I click on the button, it shows me this:

------------------------------------------------------------------------------------------------------------------------------------https://i.gyazo.com/65bb8c9fa02d007a8ed8d3d465412e4f.png


I already made a button like this before but then it worked, maybe I did something different. What should I do?


Solution

  • The error is showing that ValueError: View function did not return a response, it means your POST didn't return any response to template when you click submit button in HTML. Chagne your code flask_app.py to:

    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")
    

    This will make it works, however, it will return just the same template.