简体   繁体   中英

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. I would like to display only the first (x) number of elements, then write something and then display the last (y) elements.

I have tried to slice the list for field in l[0:x] , but I get TypeError: unhashable type: 'slice'.

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:

In Flask app: app.jinja_env.add_extension('jinja2.ext.loopcontrols')

In HTML:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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