2016-12-16 66 views
0

晚上給大家看這篇文章。 我想一個選擇框添加到燒瓶我的網站,但我不明白如何設置HTML爲 我期待着看到任何意見和建議:)如何爲selectfield(wtforms)燒瓶配置html?

我的Python代碼:

class selectmenu(Form): 
    month = SelectField('Choose month',choices=[('dec', 'dec'), ('yan', 'yan'), ('feb', 'febt')]) 

@app.route('/searchemp/', methods=['GET', 'POST']) 
def searchemp(): 
    form = selectmenu(request.form) 
    m = form.month.data 

HTML:

<form action="" class="form-signin" method="post"> 
 
        <h2 class="form-signin-heading" align="center">title</h2> 
 
        <input type="text" class="form-control" 
 
         placeholder= "username" name="username" value="{{request.form.username}}" required autofocus> 
 
           <!-- 
 
           <input type="text" class="form-control" 
 
               placeholder= "month" name="month" value="{{request.form.month}}"> 
 
           --> 
 
                 <select name="month"> 
 
                 <option value="{{request.form.month}}">dec</option> 
 
                 <option value="{{request.form.month}}">yanuary</option> 
 
                 <option value="{{request.form.month}}">feb</option> 
 
                 <option value="{{request.form.month}}">mar</option> 
 
                 </select> 
 
    
 
        <button class="btn btn-lg btn-success btn-block" type="submit">Search</button> 
 
        <br> 
 
        <p align="center">{{error}} </p> 
 
       </form>

+0

缺少' MYGz

+0

我編輯了這個問題。你能給出任何建議如何做到這一點? – trm0808

回答

0

Jinja2的模板工程ine會提供selectfield選項,你不必創建一個html選擇字段,jinja2已經做到了。如果你需要檢查表單提交使用validate_on_submit()request.method == 'POST'

class SelectMenu(Form): 
    month = SelectField('Select Month', choices=[(1, 'January'), (2,'February')]) 

@app.route('/searchemp/', methods=['GET', 'POST']) 
def searchemp(): 
    form = SelectMenu(request.form) 
    if form.validate_on_submit(): 
     # get posted data 
     m = form.month.data 
    return render_template('index.html', form=form) 

# index.html 
<form action="" method="POST"> 
    {{form.month.label}}{{form.month}} 
</form>