2015-09-26 37 views
0

我是新来的烧瓶,并且遇到困难,保存从四种不同的咖啡烘焙选项中选择的值并将其显示在另一页上。一个页面(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

你只需要从request.form对象抢元素:

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

它涵盖的所有quickstart guide