我试图让它发挥作用。我的index.html上有多个提交表单。根据我点击的内容,我希望它在我的server.py文件中运行正确的if / elif语句。目前,它只适用于" farm"出于显而易见的原因,因为我有建筑物=形成"农场"。如何根据我单击的提交按钮将其设置为适合的语句?
@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)`˚`
答案 0 :(得分:1)
在每个表单中添加一个隐藏文本字段,通过查看python代码中提交时隐藏文本字段的值来标识提交的表单。
<form action="/stuff.py">
<input type="hidden" name="building" value="casinos">
<input type="submit" value="Submit">
</form>
在烧录代码中,您只需查找隐藏文本字段并查看它具有的值,而不是按elif
查找唯一命名的组件:
@app.route("/")
def index():
session["my_gold"]=0
return render_template("index.html")
@app.route("/process_money", methods=["post"])
def money():
if request.form["building"] == "casinos":
print "I'm gonna get you"
elif request.form["building"] == "caves":
print "Bearodactyl, run!"
答案 1 :(得分:0)
以下是几个选项:
在每个表单中放入一个隐藏的输入,以发送它的形式:
<input type="hidden" name="building" value="houses">
然后您可以针对该值运行if
语句。
或强>
将每个表单发送到自己的网址。然后在烧瓶中设置独立处理每种形式的路线:
@app.route("/process_houses", methods=["post"])