Django template tags beginner

久未见 提交于 2019-12-11 16:33:39

问题


How can I do this

Example.html

{% for number in numbers %}

    {{ number }}  ##### 1
    {{ Form1 }}   ##### first loop

    {{ number }} ##### 2
    {{ Form2 }}  ##### second loop

{% endfor %}

Form1, Form2... have been passed though views


回答1:


The easiest place to fix this might be in your view. Zip the numbers and forms together:

numbers = [1,2]
forms = [Form1, Form2]

numbers_and_forms = zip(numbers, forms)

Then in your template you loop through them together.

{% for number, form in numbers_and_forms %}
  {{ number }}
  {{ form }}
{% endfor %}


来源:https://stackoverflow.com/questions/48402667/django-template-tags-beginner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!