如何使用Flask和HTML从python列表创建下拉菜单 [英] How to create dropdown menu from python list using Flask and HTML
本文介绍了如何使用Flask和HTML从python列表创建下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用python脚本中的信息在HTML中创建一个下拉菜单.到目前为止,我已经可以使用它了,但是html下拉列表将列表中的所有4个值显示为4个选项.
I'm trying to create a dropdown menu in HTML using info from a python script. I've gotten it to work thus far, however, the html dropdown displays all 4 values in the lists as 4 options.
当前:选项1 :红色,蓝色,黑色橙色; 选项2:红色,蓝色,黑色,橙色等(链接中的屏幕截图) 当前
Current: Option 1: Red, Blue, Black Orange; Option 2: Red, Blue, Black, Orange etc. (Screenshot in link) Current
期望:选项1:红色 选项2:蓝色 等等
Desired: Option 1: Red Option 2: Blue etc.
如何做到这一点,以便将python列表分隔开?
How do I make it so that the python list is separated?
dropdown.py
dropdown.py
from flask import Flask, render_template, request
app = Flask(__name__)
app.debug = True
@app.route('/', methods=['GET'])
def dropdown():
colours = ['Red', 'Blue', 'Black', 'Orange']
return render_template('test.html', colours=colours)
if __name__ == "__main__":
app.run()
test.html
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown</title>
</head>
<body>
<select name= colours method="GET" action="/">
{% for colour in colours %}
<option value= "{{colour}}" SELECTED>{{colours}}</option>"
{% endfor %}
</select>
</select>
</body>
</html>
推荐答案
您有错字,将colours
替换为colour
<option value= "{{colour}}" SELECTED>{{colours}}</option>"
替换为
<option value= "{{colour}}" SELECTED>{{ colour }}</option>"
<!-- ^^^^ -->
这篇关于如何使用Flask和HTML从python列表创建下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文