简体   繁体   English

如何在flask中切片formfield的字段列表?

[英]How can I slice a fieldlist of formfields in flask?

The code section below takes a subform (fieldlist of formfield) and loops through each of the fields and displays them in html. 下面的代码部分采用一个子窗体(formfield的字段列表),并遍历每个字段并以html显示它们。 I would like to display only the first (x) number of elements, then write something and then display the last (y) elements. 我只想显示前(x)个元素,然后写点东西,然后显示后(y)个元素。

I have tried to slice the list for field in l[0:x] , but I get TypeError: unhashable type: 'slice'. 我试图对for field in l[0:x]的列表for field in l[0:x]切片,但得到TypeError:无法散列的类型:'sl​​ice'。

Any help would be appreciated, please let me know if I have not provided enough information or context in the questions and I will provide any addition information required! 任何帮助将不胜感激,如果我在问题中没有提供足够的信息或上下文,请告知我,我将提供所需的任何其他信息!

{% for l in form.systems %}
<div class="column">
    <b> System {{ loop.index }} </b>
    <table>
        {% for field in l %}
        <tr>
            <td>
                {{field.label}}
            </td>
            <td>
                {{field(size=20)}}
            </td>  
        </tr>                         
        {% endfor %}
    </table>
</div>
{% endfor %}

在模板[:]中此切片无效时,请尝试以下操作

{{ your_list|slice:"0:x" }}

I was able to access parts of the loop using loop controls from jinja: 我可以使用jinja中的循环控件来访问循环的一部分:

In Flask app: app.jinja_env.add_extension('jinja2.ext.loopcontrols') 在Flask应用中: app.jinja_env.add_extension('jinja2.ext.loopcontrols')

In HTML: 在HTML中:

                        {% for field in l %}
                            {% if loop.index < 5 %}
                            <tr>
                                <td>
                                    {{field.label}}
                                </td>
                                <td>
                                    {{field(size=20)}}
                                </td>  
                            </tr>
                            {% endif %}                                       
                        {% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM