使用Jinja从列表动态填充下拉列表 [英] Populating dropdown dynamically from list with Jinja

查看:113
本文介绍了使用Jinja从列表动态填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flask应用程序,该应用程序从其路由之一生成一个数据列表,然后将其显示在前端的下拉菜单中.问题是,每个列表中将存在不同数量的项目,这些项目将填充相同的下拉菜单.例如,一个用户帐户可能有三个项目,而另一个用户帐户可能有二十个项目.

I have a Flask application which, from one of its routes, produces a list of data that I would then like to display in a dropdown menu on the front-end. The issue is, a different number of items will exist in each list that will populate the same dropdown menu. For example, one users accounts might have three items, while another's might have twenty.

我的路线如下:

@app.route("/test", methods=['GET', 'POST'])
def test():

    #list with 10 items is generated here



return render_template('test.html', title="test", list=list)

我的HTML看起来应该像这样:

My HTML should look something like:

{% for x in list %}
<select>
<option value="tester">tester1</option>
<option value="tester2">tester2</option>
<option value="tester3">tester3</option>
<option value="tester4">tester4</option>
</select>
{% endfor %}

我传递给模板的列表中有十个项目,如第一个代码部分中的注释所示(这是动态的-下次可以是任何数字),下拉菜单中的选项数目是静态的.动态填充此下拉列表的最佳编程解决方案是什么(请不要包括涉及EXCEL或数据库的答案)?

My list that I've passed to the template has ten items as indicated in the comment in my first code section (this is dynamic - could be any number next time), and my number of options for my drop down is static. What is the best programmatic solution for dynamically populating this dropdown (please do not include answers involving EXCEL or a database)?

推荐答案

您可以使用以下语法来循环列表

you can use the following syntax to loop your list

<select>
    {% for x in list %}
    <option value="{{x.id}}">{{x.text}}</option>
    {% endfor %}
</select>

这篇关于使用Jinja从列表动态填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆