簡體   English   中英

在 django 中按天分組文章

[英]group articles by day in django

我每天都在嘗試對文章進行分組,圖像打擊將向您展示它現在的樣子

在此處輸入圖像描述

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

您可以嘗試使用此答案中提到的{% 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 %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM