设置特定日期以在django模板中打印特定消息

时间:2016-08-08 06:00:25

标签: django django-templates

这是我的模特:

class Sitting(models.Model):
    sit_date = models.DateField(blank=False)
    cut_off_date = models.DateField(null=True, blank=True)
    ballot_date = models.DateField(null=True, blank=True)
    sess_no = models.ForeignKey(Session,
                                 on_delete=models.CASCADE)
    genre = TreeForeignKey('Genre', null=True, blank=True, db_index=True)

这是我的观点:

def sitting_list(request):
    sitting=Sitting.objects.select_related('genre')

    return render(
        request,
        'genre/sitting_list.html',
        {'sittings':sitting, },  )

这是我的模板:

{% block content %}
<table>
    <tr>
  <th>Sitting Day & Date</th>
  <th>Ministry/Division</th>
  <th>Ballot Date</th>

    </tr>
    {% for sitting in sittings %}
    <tr>
       <td> {{ sitting.sit_date|date:"l, d F, Y" }}</td>

    {% for genre in sitting.genre.get_descendants %}

             <td> {{ genre }},</td>

        {% endfor %}
        <td>(Ballot: {{ sitting.ballot_date}})</td>
    </tr>
        <tr>
    </tr>
    {% endfor %}
</table>
{% endblock %}

它提供了以下输出:

enter image description here

编辑:

但我想要关注输出:

enter image description here

你可以从我想要的输出中看到,只有当没有分区时才会出现魔法部的名字。编号和最后一次完全停止也不会到来。

我希望在你的帮助下也可以解决这个问题。

1 个答案:

答案 0 :(得分:0)

你只需要添加一个if语句来查看weekday是否为2(0-6 mon-sun)

 <td> 
{% if sitting.sit_date.weekday == 2 %}
      extra stuff
 {% endif %}
 {% for genre in sitting.genre.get_descendants %}
      {{ genre }},
 {% endfor %}
</td>

注意:还移动了td,以便获得所需的格式化样式。