ssk343k
ssk343k

Reputation: 53

How to get user selected value from SelectField in python Flask WTF?

Here is the code snippet I have written

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 %}

I am creating many drop down lists in home.html How can I get the data that the user selects from each of the drop down list and print it ? I am trying to create a user friendly web application where the user will select the values from a drop down list and the application will fetch the data accordingly from the database.

Upvotes: 4

Views: 7909

Answers (3)

Daniel Mandelblat
Daniel Mandelblat

Reputation: 337

I sure that you was find your solution, but for next generation. You need insert in the select tag the name of the value, and not in options tags.

example:

<select name="sServer">

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

</select>

Upvotes: 0

Attack68
Attack68

Reputation: 4765

@app.route("/")
@app.route("/home", methods=['GET', 'POST'])
def home():
    forms = SearchForm()
    if request.method == 'POST':
        print(forms.field_name.data) # <- prints to console.
        # to print to web convert to variable and render with jinja2
        # return render_template('display.html', field_name=forms.field_name.data)
    return render_template('home.html', title='Home', forms=forms)

Upvotes: 3

JKimbrough
JKimbrough

Reputation: 236

Have you tried transforming the 'choices' into a dict? You can then get the value for the key form data. Maybe something like this (for your "status" SelectField):
value = dict(form.status.choices).get(form.status.data)

Upvotes: 2

Related Questions