首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在html模板中使用for循环

如何在html模板中使用for循环
EN

Stack Overflow用户
提问于 2019-05-23 17:49:35
回答 1查看 5.4K关注 0票数 0

我尝试在html模板中使用for循环来制作表格,但他添加的probleme总是列在表格之外,如这张图片所示

我不知道他为什么继续直接走出表(如果只有一个对象,这是可以的,但当有超过1个对象在ligne中时,就会发生这种情况)

我能做些什么?

谢谢

html模板

代码语言:javascript
复制
<tr class="col-2">
    <th>JOUR</th>
    <th>8-10</th>
    <th>10-12</th>
    <th>12-14</th>
    <th>14-16</th>
    <th>16-18</th>
</tr>


<tr class="col-2">
    <td>SAMEDI</td>

             {% for ins in ob %}
    {%  if ins.jour = 'S' %}
    {% if ins.heur = '1' and ins.jour = 'S' %} <td>{{ins}}</td> {% else  %} <td>    </td> {% endif %}
    {% if ins.heur = '2' and ins.jour = 'S' %} <td>{{ins}}</td>{% else  %} <td>    </td> {% endif %}
    {% if ins.heur = '3' and ins.jour = 'S' %} <td>{{ins}}</td>{% else  %} <td>    </td> {% endif %}
    {% if ins.heur = '4' and ins.jour = 'S' %} <td>{{ins}}</td>{% else  %} <td>    </td> {% endif %}
    {% if ins.heur = '5' and ins.jour = 'S' %} <td>{{ins}}</td>{% else  %} <td>    </td> {% endif %}

    {% endif %}
    {% endfor %}

 {% if a != 2 %}

     <td></td>
             <td></td>
     <td></td>
     <td></td>
     <td></td>
    {% endif %}
</tr>

views.py

代码语言:javascript
复制
def tempss(request):
ob=temps.objects.all()
a=1
b=1
c=1
d=1
e=1
f=1
for ins in ob:
    if ins.jour=='S':
        a=2
    elif ins.jour=='D':
        b=2

model.py

代码语言:javascript
复制
class temps(models.Model):
JOUR_CHOICES = (
    ('S', 'Samedi'),
    ('D', 'Dimanche'),
    ('L', 'Lundi'),
    ('M', 'Mardi'),
    ('R', 'Mercredi'),
    ('J', 'Jeudi'),
    ('V', 'Vendredi'),
)
HEUR_CHOICES = (
    ('1', '8-10'),
    ('2', '10-12'),
    ('3', '12-14'),
    ('4', '14-16'),
    ('5', '16-18'),
)
jour = models.CharField(max_length=1, choices=JOUR_CHOICES)
heur = models.CharField(max_length=1,choices=HEUR_CHOICES)
salle=models.ForeignKey(salle,on_delete=models.CASCADE)
groupe=models.ForeignKey(groupe,on_delete=models.CASCADE,limit_choices_to={'any_field':False},)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-23 18:34:44

你的循环

代码语言:javascript
复制
{% for ins in ob %}

将为每个不满足您定义的条件的对象呈现一个空表数据标记。

这部分是多余的,因为您已经使用相同的条件进行了封装:

代码语言:javascript
复制
and ins.jour = 'S' %}

然而,当你想要创建表格(它有固定的行数和单元格),并将条件放入单元格时,和更有意义-我相信有一个更优雅的解决方案,但考虑到视图和模型,这就是我想出来的:

代码语言:javascript
复制
<td>{% for ins in obj %}{% if ins.jour = 'S' and ins.heur = '1' %}{{ins}}
{% endif %}{% endfor %}</td>

对其余的时隙进行相应的重复。也许可以考虑在视图中而不是在模板中构造表值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56272517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档