Setting specific day to print specific message in the django template

泪湿孤枕 提交于 2019-12-12 04:30:58

问题


Here is my model:

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)

here is my view:

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

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

Here is my template:

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

It gave following output:

Edit :

But I want following output:

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.


回答1:


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>

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



来源:https://stackoverflow.com/questions/38822165/setting-specific-day-to-print-specific-message-in-the-django-template

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