2017-02-27 82 views
0

我正在創建一個由jinja for循環創建的列表頁面。 我希望用戶選擇該列表上的項目。 我希望該選擇可以將用戶發送到該項目頁面,並將選定的項目發送回我的Python代碼以供進一步處理。燒瓶發送html數據返回查看

在我'home.html的頁面我有以下幾點:

{% for item in list %} 
    {{ item }} 
    <form method='POST'> 
     <input type='submit' value='select'> 
    </form> 
{% endfor %} 

然後在我的蟒蛇觀點:

@app.route('/', methods=['GET','POST']) 
def home(): 
    list = ['a','b','c'] 

    #??????????????????????????????????????????????? 
    selected = request.form.item 

    return render_template('home.html', list=list) 

回答

1

你需要單獨處理的GETPOST方法您的home()路線。事情是這樣的:

@app.route('/', methods=['GET','POST']) 
def home(): 
    if request.method == 'POST': 
     selected = request.form.item 
     # on this line you can process the selected item, but you haven't 
     # stated how you'll do that, so I don't know what to display here 
     return redirect(url_for('item_page.html', item=selected)) 
    else: 
     list = ['a','b','c'] 
     return render_template('home.html', list=list) 

很顯然,我還以爲你有,你用它來顯示個人項目中選定的用戶模板item_page.html。如果不是,則替換您將在此使用的任何模板。您還需要一個@app.route來處理該模板。