在django模板中设置特定日期以打印特定消息 [英] Setting specific day to print specific message in the django template

查看:187
本文介绍了在django模板中设置特定日期以打印特定消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型:

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 %}

它提供以下输出:

编辑:

但是我需要以下输出:

你可以从我想要的输出中看到,部门的名字只有在没有分裂的情况下才会出现。此外,编号和上一个完整的停止也不会来。

You can see from my desired output, Ministry's name only come when no division exists. Moreover numbering and last full stop also not coming.

我希望这也可以用你的帮助来解决。

I hope this can also be fixed with your help.

推荐答案

您只需添加一个if语句,查看 weekday 是2(0-6 mon-sun)

You just need to add an if statement to see if the weekday is 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,以便获得所需的格式化样式。

Note: Also moved the td so you get the formatting style you wanted.

这篇关于在django模板中设置特定日期以打印特定消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆