說我有一個名爲seq的python列表,我想在選擇元素中呈現它,我該怎麼做?如何循環使用jinja2 for循環來創建一個select元素?
我想:
<select name="Exercise1">
{% for item in seq %}
<option value="{{item}}">{{item}}</option>
{% endfor %}
</select>
沒有奏效。
UPDATE: 這裏是模板代碼:
<!DOCTYPE html>
<html>
<head>
<title>Unit 2 Rot 13</title>
</head>
<body>
<h2>Enter some text to ROT13:</h2>
<form method="post">
<select name="Exercise1">
{% for item in seq %}
<option value="{{item}}">{{item}}</option>
{% endfor %}
</select>
<br>
<input type="submit">
</form>
</body>
</html>
而這裏呈現的HTML源代碼:
<!DOCTYPE html>
<html>
<head>
<title>Unit 2 Rot 13</title>
</head>
<body>
<h2>Enter some text to ROT13:</h2>
<form method="post">
<select name="Exercise1">
</select>
<br>
<input type="submit">
</form>
</body>
</html>
我不知道爲什麼元素就會消失,也沒有引發錯誤。
它以什麼方式「不工作」?您的模板看起來很好(假設所有內容都已正確轉義)。 – Cameron 2013-02-20 22:56:34
好吧,select元素不會出現在呈現的html中。 – camara90100 2013-02-20 23:16:57
你能舉一個'seq'包含的例子嗎?如果你把一個虛擬鏈接放在選擇上面,另一個在右邊,他們會顯示出來嗎? – Cameron 2013-02-21 00:21:46