如何用Flask填充选择标签? [英] How do I populate a select tag with Flask?

查看:107
本文介绍了如何用Flask填充选择标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在python中访问JSON对象列表(来自mongo),以使用烧瓶模板填充下拉列表.随后,我需要访问选定的项目.我很难填充列表.这是我的python代码和模板.谁能指出我正确的方向?我没有找到有关如何使用这些模板填充dd列表的好的文档.

I am trying to access a list of JSON objects (from mongo) in python to populate a dropdown list using flask templates. Subsequently, I need to access the selected item. I am having difficulty getting the list to populate. Here's my python code and template. Can anyone point me in the right direction? I have not been able to find good documentation on how to populate the dd list with these templates.

PYTHON:

@app.route('/page', methods=["GET", "POST"])
@login_required
def chooser():
    # Option list returns a list of JSON objects 
    option_list = get_options(g.user)
    # {u'_id': ObjectId('52a347343be0b32a070e5f4f'), u'optid': u'52a347343be0b32a070e5f4e'}

    # for debugging, checks out ok
    print option_list

    # Get selected id & return it
    if request.form['submit'] == 'Select':
            optid = o.optid
            resp = 'You chose: ', optid
            return Response(resp)

    return render_template('chooser.html')

HTML模板:

{% extends "layout.html" %}
{% block content %}
    <h2>Chooser<h2>
        <h3><table><form action="" method="POST">
          <td>
          <label>Select :</label>
            <select name="option" width="300px">
              {% for o in option_list %}
                <option name="{{ o.optid }}" SELECTED>{{ o.optid }}</option>
              </select>
              </td>
            <td>
              <input class="button1" type="submit" value="Select">
            </td>
              {% endfor %}
        </form></table><h3>

  </div>
{% endblock content %}

推荐答案

这里有些错误:

首先,当您调用render_template('chooser.html')时,您永远不会将任何参数传递给模板呈现过程.默认情况下,如果找不到您引用的属性,Jinja2不会出错,因此您不会收到任何错误消息.基本上,对于模板中的代码,如下所示:

First, when you call render_template('chooser.html'), you are never passing any arguments to the template rendering process. By default, Jinja2 does not error out if an attribute your reference is not found, so you get no error message. Basically, for the code in your template that looks like this:

{% for o in option_list %}
    ....
{% endfor %}

此for循环的内部永远不会呈现,因为您永远不会指定option_list是什么.在这种情况下,Jinja2只会将其默认为一个空字符串,然后您实际上是在遍历该空字符串的字符(这当然意味着永远不会运行for循环的内部).

The innards of this for loop are never rendered because you never specify what option_list is. In this case, Jinja2 will just default it to an empty string, and then you are essentially looping over the characters of the empty string (which of course means the innards of the for-loop are never run).

因此,您需要指定模板option_list的值为:

So, you need to specify the template what the value of option_list is:

return render_template('chooser.html', option_list=option_list)

另一个问题是,由于您放置了for循环,您的HTML将会被弄乱:

The other issue is that your HTML is going to be messed up due to where you've put your for loop:

<select name="option" width="300px">
{% for o in option_list %}
    <option value="{{ o.optid }}" SELECTED>{{ o.optid }}</option>
    </select>
</td>
<td>
    <input class="button1" type="submit" value="Select">
</td>
{% endfor %}

我认为您想执行以下操作:

I think you wanted to do the following:

<select name="option" width="300px">
{% for o in option_list %}
    <option value="{{ o.optid }}" SELECTED>{{ o.optid }}</option>
{% endfor %}
</select>
</td>
<td>
    <input class="button1" type="submit" value="Select">
</td>

请参见教程的呈现部分,或 Jinja2文档以获取更多信息.

See the rendering part of the tutorial, or the Jinja2 documentation for more info.

这篇关于如何用Flask填充选择标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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