如何从python Flask WTF中的SelectField获取用户选择的值?

时间:2018-08-08 22:10:27

标签: python flask flask-wtforms

这是我编写的代码段

MainPage.py

from flask import Flask, render_template,url_for,request, redirect
from form import SearchForm
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secretkey1234'


@app.route("/")
@app.route("/home", methods=['GET', 'POST'])
def home():
    forms = SearchForm()
    return render_template('home.html', title='Home', forms=forms)

@app.route("/about")
def about():
    return render_template('about.html', title='About')


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

form.py

from flask_wtf import FlaskForm
import Get_Test_Suite
from wtforms.fields import SelectField, SubmitField
from wtforms.fields.html5 import DateField
from wtforms.validators import DataRequired


class SearchForm(FlaskForm):
    test_suite_list = Get_Test_Suite.get_test_suite()
    suite_list = []
    for i in test_suite_list:
        suite_list.append((i, i))
    test_suite_selected = SelectField('Test Suite Name', choices=suite_list)

    test_module_list = Get_Test_Suite.get_module_name()
    module_list = []
    for j in test_module_list:
        module_list.append((j, j))
    test_module_selected = SelectField('Test Module Name', choices=module_list,validators=[DataRequired()])
    date_selected = DateField('Date', format='%m-%d-%Y')
    status = SelectField('Status', choices=[('Active', 'Active'), ('Closed', 'Closed')])
    submit = SubmitField('Submit')

home.html

{% extends "layouts.html" %}
{% block content %}
   <div class = "content-section">
       <form method="POST" action="">
           {{forms.hidden_tag()}}
           <fieldset class="form-group">
               <legend class ="border-bottom mb-4">
                   <center>SEARCH TEST FAILURE STATUS</center>
               </legend>
               <div class="form-group">
                    {{forms.test_suite_selected.label(class="form-control-label")}}
                    {{forms.test_suite_selected(class="form-control form-control-lg")}}
               </div>
               <div class="form-group">
                    {{forms.test_module_selected.label(class="form-control-label")}}
                    {{forms.test_module_selected(class="form-control form-control-lg")}}
               </div>
               <div class="form-group">
                    {{forms.date_selected.label(class="form-control-label")}}
                    {{forms.date_selected(class="form-control form-control-lg")}}
               </div>
               <div class="form-group">
                    {{forms.status.label(class="form-control-label")}}
                    {{forms.status(class="form-control form-control-lg")}}
               </div>
           </fieldset>
            <div class="form-group">
                    {{forms.submit(class="btn btn-outline-info")}}
            </div>
       </form>
   </div>
{% endblock content %}

我正在home.html中创建许多下拉列表 如何获得用户从每个下拉列表中选择的数据并进行打印?我正在尝试创建一个用户友好的Web应用程序,用户将在其中从下拉列表中选择值,然后该应用程序将从数据库中相应地获取数据。

3 个答案:

答案 0 :(得分:1)

git reset

答案 1 :(得分:0)

您是否尝试过将“选择”转换成字典?然后,您可以获取密钥表单数据的值。也许是这样的(对于您的“状态” SelectField):
value = dict(form.status.choices).get(form.status.data)

答案 2 :(得分:0)

我确定您已找到解决方案,但适用于下一代。 您需要在选择标签中插入值的名称,而不是在选项标签中。

示例:

<select name="sServer">

  {% for server in servers %}
  <option  value="{{server}}">{{server}}</option>
  {% endfor %}

</select>