我每天都在嘗試對文章進行分組,圖片吹送將向您展示現在的樣子

我有 3 條記錄,它們的日期位于右上角,但正如您所看到的,每條記錄都是分開的,我想在一組中顯示此日期的記錄,如果我在該日期組中添加更多記錄,則明天顯示,我的代碼:
模型.py:
class Form(models.Model):
food_type = models.ForeignKey(FoodTypes, blank=False, null=True, on_delete=CASCADE)
calorie = models.CharField(max_length=50)
protein = models.ForeignKey(Protein, blank=False, null=True, on_delete=CASCADE)
carbohydrate = models.ForeignKey(Carbohydrate, blank=False, null=True, on_delete=CASCADE)
drinks = models.ForeignKey(Drinks, blank=False, null=True, on_delete=CASCADE)
fruits = models.CharField(max_length=50)
note = models.CharField(max_length=200, blank=True)
date = models.DateField(default=datetime.date.today)
視圖.py:
def requests(request):
lists = Form.objects.all().order_by('-date')
context = {
'lists': lists
}
return render(request, 'form/requests_list.html', context)
模板檔案:
{% for lists in lists %}
<div class="req-list">
<h6>{{ lists.date }}</h6>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">??? ???</th>
<th scope="col">?????</th>
<th scope="col">???????</th>
<th scope="col">??????????</th>
<th scope="col">???????</th>
<th scope="col">????</th>
<th scope="col">???????</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">{{ lists.id }}</th>
<td>{{ lists.food_type }}</td>
<td>{{ lists.calorie }}</td>
<td>@{{ lists.protein }}</td>
<td>@{{ lists.carbohydrate }}</td>
<td>@{{ lists.drinks }}</td>
<td>@{{ lists.fruits }}</td>
<td>@{{ lists.note }}</td>
</tr>
</tbody>
</table>
</div>
{% endfor %}
uj5u.com熱心網友回復:
您可以嘗試使用此答案中提到的{% ifchanged %}標簽:https : //stackoverflow.com/a/3986324/4151233
以下代碼未經測驗:
{% for lists in lists %}
{% ifchanged lists.date %}
<div class="req-list">
<h6>{{ lists.date }}</h6>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">??? ???</th>
<th scope="col">?????</th>
<th scope="col">???????</th>
<th scope="col">??????????</th>
<th scope="col">???????</th>
<th scope="col">????</th>
<th scope="col">???????</th>
</tr>
</thead>
<tbody>
{% endifchanged %}
<tr>
<th scope="row">{{ lists.id }}</th>
<td>{{ lists.food_type }}</td>
<td>{{ lists.calorie }}</td>
<td>@{{ lists.protein }}</td>
<td>@{{ lists.carbohydrate }}</td>
<td>@{{ lists.drinks }}</td>
<td>@{{ lists.fruits }}</td>
<td>@{{ lists.note }}</td>
</tr>
{% ifchanged lists.date %}
</tbody>
</table>
</div>
{% endifchanged %}
{% endfor %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371710.html
