1

假设我有一个名为 seq 的 python 列表,我想在选择元素中呈现它,我该怎么做?

我试过了:

<select name="Exercise1">
         {% for item in seq %}
            <option  value="{{item}}">{{item}}</option>
         {% endfor %}
         </select>

它没有用。

更新:这是模板代码:

<!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>

我不确定为什么元素会消失并且没有引发错误。

4

2 回答 2

10

你很亲密,我的朋友,你只是错过了几个空格:

你写了这个:

<option  value="{{item}}">{{item}}</option>

这需要看起来像这样:

<option value="{{ item }}">{{ item }}</option>

让我知道是否可以解决它。祝你好运!

如果没有这些空格,它对 python 和浏览器来说就像垃圾:)

于 2013-03-19T20:42:42.353 回答
0

检查渲染模板的函数,它必须包含:

def generate_template_for_exercize1():

    seq = [1, 2, 3] #here you probably will have more complex statement

    return render_template('your_template.html', seq=seq)
于 2019-08-03T19:59:41.673 回答