Flask从字段中保存选定的值并将其显示在另一个页面上

时间:2015-09-26 21:14:28

标签: python html flask

我是Flask的新手,我很难保存从四种不同咖啡烘焙选项中选择的价值,并将其显示在另一页上。一页(Roast.html)用户可以选择从Light,Medium,Dark和Spe​​cial中选择一种咖啡。按下提交按钮后,我想在下一页(Brew.html)上显示这个选项,即“你的_____烘焙咖啡正在酝酿!”任何帮助将不胜感激!我不想在这段代码中涉及php或javascript。谢谢

coffeeMaker.py:

from flask import Flask
from flask import render_template
from flask import url_for
from flask import request

app = Flask(__name__)

@app.route('/')
def welcome():
    return render_template("welcome.html")

@app.route('/Roast', methods = ['GET','POST'])
def roast():
    print "inside Roast"
    return render_template("Roast.html")

@app.route('/Brew', methods = ['GET','POST'])
def brew():
    if request.method == 'POST':
        print 456
        a = request.query_string
        print 789
        return render_template("Brew.html",selection = a )   
    else:
        return "This is a GET request."



if __name__ == '__main__':
    app.run()

welcome.html:

<html>
<head>
<title>Welcome to Coffee Maker</title>
</head>
<body>
<h1>This is a website for you to order your coffee of perference
</h1>
<a href = "/Roast" >ready for a coffee</a>
</body>
</html>

Roast.html:

<html>
<form action="/Brew" method="Post">    
  <h3>2. Choose your Roast</h3>
  <input type="text" name="firstname" value="Mickey">
  <br>
  <input type="radio" name="roastType" value="Light">Light
  <br>
  <input type="radio" name="roastType" value="Medium" checked>Medium
  <br>
  <input type="radio" name="roastType" value="Dark">Dark
  <br>
  <input type="radio" name="roastType" value="HackGT Blend">Special Blend
<br><br>
<input type="submit" value="Brew It">
</form>
</html>

Brew.html:

<html>
<head>
<title>
brew
</title>
</head>
<body>
<h1> Brewing</h1>
<p>You selected{{selection}}</p>
<p>Get ready for your perfect cup of coffee~</p>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您只需要从request.form对象中获取元素:

if request.method == 'POST':
    brew_type = request.form["roastType"]
    print(brew_type)
    return render_template("Brew.html", selection=brew_type)

quickstart guide已涵盖所有内容。