如何使用Python和Flask在代码中获取引导程序下拉列表的选定值?

问题描述 投票:0回答:1

我是Python和Flask编程的新手。在我的网站开发中,我试图获取从引导单按钮下拉列表中选择的python代码中的值。这是我尝试过的-

Python代码-routes.py

   @app.route("/index", methods=['GET','POST'])
   def index():
       form = IndexForm()
       data = ['Red', 'Green', 'Blue']
       if form.validate_on_submit():
           posts = get_data(color=request.form.get("color_dropdown"))
           return render_template('index.html', title='My Form', form=form, posts=posts, data=data) 

       return render_template('index.html', title='My Form', form=form, data=data)

HTML代码-index.html

该下拉列表将按预期填充列表'data'的元素。

   <div class="dropdown">
        <div class="input-group-btn">
            <button class="btn btn-info btn-md dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" name="color_dropdown" >
            Select Color
            <span class="caret"></span>
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                {% for datum in data %}
                   <a class="dropdown-item" href="#">{{ datum }}</a>
                {% endfor %}
            </div>
        </div>
    </div>

到目前为止,一切都很好。enter image description here

[当我选择颜色选项时,例如使用jscript-

也可以在按钮上看到“红色”
   <script>
        $(".dropdown-menu a ").click(function(){
            $(this).parents(".input-group-btn").find('.btn').text($(this).text());
        });
    </script>

看起来像上面。enter image description here

现在,当我尝试使用-从下拉列表中检索所选选项的值时-color=request.form.get("color_dropdown")它总是返回None

表单类-forms.py

   class IndexForm(FlaskForm):
      car = StringField('Car', validators=[DataRequired()])
      submit = SubmitField('Submit')

如何获取routes.py中所选选项的值(在上述情况下为“红色”)并将其分配给颜色变量?预先感谢。

python flask bootstrap-4 dropdown flask-wtforms
1个回答
0
投票

通过使用语法variable = form_name.form_field.data(在routes.py中,我已经或多或少地完成了您在没有JS的情况下想要的东西。

我希望这会有所帮助。

编辑:

可能最好直接访问表单的属性。另外,

forms.py:

class IndexForm(FlaskForm):
      car_options = SelectMultipleField(
                 u'Select Color', choices=[('Red', 'red'), ('Blue', 'blue'), ('Green', 'green')], size=1)
      submit = SubmitField('Submit')

index.html:


    <form action='/' , method='POST'>
        {{form.hidden_tag() }}
        <button class="btn btn-info btn-md dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" name="color_dropdown" >
            Select Color
            <span class="caret"></span>
            </button>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        {% for subfield in form.choices %}
              <a class="dropdown-item" href="#">
              {{ subfield.label }}
              </a>
        {% endfor %}
        </div>
        <div class="submit">
            {{ form.submit(class="btn btn-info") }}
        </div>
    </form>

routes.py:

@app.route("/index", methods=['GET','POST'])
   def index():
       form = IndexForm()
       if form.validate_on_submit():

           color_choice = form.car_options.data
           if color_choice = 'green':
               print("Sick bro")
           else:
               pass

           #since you can access the choices as an attribute of the form,
           #there is no need to define "posts" (I named it color choice) unless 
           #you are going to use it here.

           return redirect('index.html')

       return render_template('index.html', title='My Form', form=form, data=data)

© www.soinside.com 2019 - 2024. All rights reserved.