0

我正在尝试创建一个自动完成功能列表以通过 Python 和 Jinja2(通过 Flask)放入我的网页

数据:

 data_list=['router1 | location - tx | model A | xxserialxx | 10.0.0.1 | ',
 'router2 | location - mo | model A | xxserialxx | 10.0.0.2 | ',
 'router3 | location - ca | model B | xxserialxx | 10.0.0.3 | ',
 'router4 | location - fl | model A | xxserialxx | 10.0.0.4 | ',
 'router5 | location - ny | model B | xxserialxx | 10.0.0.5 | ']

这是我的模板附带的默认外观:

<input class="form-control" placeholder="Type something..." type="text"
        data-autocomplete='[
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"]'>

我试过:

<input class="form-control" placeholder="Type something..." type="text"
        data-autocomplete='[{% for item in data_list %}
        "{{item}},"]' {% endfor %}>

什么也没有发生,我加载 HTML 并且“ data-autocomplete=”等于什么都没有..

这个逻辑肯定是错误的,我可以看到。

我找不到太多关于如何使用 Jinja 将 for 循环放入类似结构的列表的文档。

我认为这是完全错误的方法。帮助?

期望的输出

<input class="form-control" placeholder="Type something..." type="text"
        data-autocomplete='[
        "router1 | location - tx | model A | xxserialxx | 10.0.0.1 | ",
        "router2 | location - mo | model A | xxserialxx | 10.0.0.2 | ",
        "router3 | location - ca | model B | xxserialxx | 10.0.0.3 | ",
        "router4 | location - fl | model A | xxserialxx | 10.0.0.4 | ",
        "router5 | location - ny | model B | xxserialxx | 10.0.0.5 | ",]'>
4

1 回答 1

1

您可以使用 join 方法,甚至不需要 for 循环。试试这个:

data-autocomplete="{{'[' + ','.join(data_list) + ']'}}"
于 2014-10-05T00:39:19.693 回答