How to tell which HTML form was submitted to Flask?

Viewed 3266

I'm trying to get this to work. I have multiple submit forms on my index.html. Depending on which I click I want it to run the proper if/elif statement in my server.py file. Currently, it only works for "farm" for obvious reasons as I have buildings = to form "farm". How can I set it up to work for the appropriate statement based on which submit button I click?

@app.route("/")
def index():
    session["my_gold"]=0
    return render_template("index.html")

@app.route("/process_money", methods=["post"])
def money():

building = request.form["farms"]

if request.form["farms"] == building:
    print "hello"
    gold = random.randrange(10,21)
    print gold
    session["my_gold"] = session["my_gold"] + gold
    print session["my_gold"]

elif request.form["caves"] == building:
    print "goodbye"
    gold = random.randrange(5,11)
    print gold
    session["my_gold"] = session["my_gold"] + gold
    print session["my_gold"]

elif request.form["houses"] == building:
    print "wow"
    gold = random.randrange(2,6)
    print gold
    session["my_gold"] = session["my_gold"] + gold
    print session["my_gold"]

elif request.form["casinos"] == building:
    print "sucka"
    gold = random.randrange(0,51)
    print gold
    session["my_gold"] = session["my_gold"] + gold
    print session["my_gold"]


return redirect("/")

app.run(debug=True)`˚`
2 Answers
Related