2016-02-20 19 views
0

我想将数据发送到页面(作为单选按钮),然后记录输入。现在我有一个问题,因为我无法弄清楚如何使用烧瓶。 的Python:使用Flask发送和接收来自同一页面的数据

... 
@app.route('/', methods=['GET', 'POST']) 
def index(): 
    data = adb.fetch() 
    return render_template('index.html', data=data) 

@app.route('/', methods=['POST']) 
def handle_data(): 
    name = request.form['option'] 
    print name 

和HTML:

<form accept-charset="UTF-8" action="#" 
    class="form-horizontal" id="names" method ="post"> 
    <div class="control-group"> 
    {% for row in data %} 
    <div class="controls"> 
     <label class="radio"> 
     <input type="radio" 
     name="option" 
     id="{{row['lid']}}" 
     value="{{row['name']}}"> {{row['name']}} 
     </label> 
    </div> 
    {% endfor %} 
    </div> 
    <input type="submit" value="Submit"> 
</form> 

我得到适当的输出到现场,但结果不会打印到我的控制台。

如何在同一页上发送和接收数据?

回答

0

所以这是瓶的一个非常基本的属性,但答案是:

if request.method == 'GET': 
     return render_template('index.html', data=data) 
    elif request.method == 'POST': 
     name = request.form['option'] 
     print name 
     return "Page submitted" 
相关问题