Django在同一个temp中为不同的for循环重用html块

2024-10-04 01:28:44 发布

您现在位置:Python中文网/ 问答频道 /正文

对于给定的对象,我有三个条件,1。要启动2。开始,和3。已完成。 我过滤视图中的对象,并向模板发送三个变量- tostart_objectsstarted_objects和{}。在

现在,我在html模板中循环三个for循环,如下所示:

{% for obj in tostart_objects %}
// chunk of html template to display all the object 
{% endfor %}

{% for obj in started_objects %}
// similar chunk of html template as above
{% endfor %}

{% for obj in finished_objects %}
// similar chunk of html template as above
{% endfor %}

我可以不把相同的代码块放在三个不同的地方重用吗? 如何做到这一点?请告诉我,谢谢!在


Tags: of对象in模板objforobjectshtml
1条回答
网友
1楼 · 发布于 2024-10-04 01:28:44

一种方法是将HTML块放在一个单独的模板文件中,并使用{% include %}将其包含在每个循环中。在此基础上,可以将整个循环定义为以对象列表为参数的包含模板标记。在

或者,可以将视图中的三个列表串联起来,然后循环查看结果。在

相关问题 更多 >