Forums

Flask POST variables and redirect not working

I'm trying to create a flask site which gets the value of a drop down box and the string in a text box after a button click. Then redirect to another html page I made. I've been following the beginners guide to building a flask website from this site to do this (modifying it to my purposes) but I'm getting an error. HTML:

             <form action="." method="POST">
                <div class="col-sm-4" id="mainsubdiv">
                    <select class="form-control" id="op" name="choice">
                        <option disabled selected value></option>
                        <option value="1">Basic Algebra</option>
                        <option value="2">Quadratics</option>
                        <option value="3">Simultaneous equations</option>
                        <option value="4">Differentiation</option>
                    </select>
                </div>
                <div class="col-sm-6" id="mainsubdiv">
                    <input type="text" class="form-control" id="textbx" name="equation">
                </div>
                <div class="col-sm-2" id="mainsubdiv">
                    <button type="button" class="btn btn-default" href="http://google.com" id="subbtn">Submit <span class="glyphicon glyphicon-ok"></span></button>
                </div>
            </form>

FLASK:

from flask import Flask, redirect, render_template, request, url_for                             
app = Flask(__name__)

app.config["DEBUG"] = True



@app.route("/", methods=["GET", "POST"])

def index():
    if request.method == "GET":
        return render_template("main_page.html")

choice = request.form["choice"]
equation = request.form["equation"]
return redirect(url_for("answer_page.html")                         <-- syntax error here

Any help is appreciated :)

EDIT: The syntax error has now been solved meaning my main_page.html is loading but when i click my submit button the redirect ins't working. Any ideas? Is my form for POST completely wrong?

It looks like the last three lines of that code should be indented one level.

I've indented the three lines and I'm still getting the something went wrong screen and the syntax error

Have you reloaded the web app since you made the source code change?

You are missing a closing parentheses. redirect(url_for("answer_page.html"))

The extra parentheses solved one of the problems, Thanks :) . My home page is now showing (main_page.html) but when i click my submit button it ins't redirecting. Any ideas? Or is my logic completely off? thanks again

Is your browser actually sending a POST when you click your button?

I'm new to html but I believe your button needs to be <button type="submit">. According to here https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button a submit button is needed to pass form data. A button type button could send form data but you would need to manually script it to which is beyond my knowledge.

Thanks that worked :) Thank you all for your help i really do appreciate it!

Glad you got it sorted!