【问题标题】:group articles by day in django在 django 中按天分组文章
【发布时间】:2022-01-08 09:01:20
【问题描述】:

我每天都在尝试对文章进行分组,图片会告诉你现在的样子

我有 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)

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

【问题讨论】:

    标签: python django


    【解决方案1】:

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

    【讨论】:

    • 确实有效,非常感谢,但它会像这样 {% ifchanged lists.date %}
      {{ lists.date }}
      ​​> {% endifchanged %} only 1 ifchanged for lists.date
    猜你喜欢
    • 2016-09-15
    • 2012-04-26
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2021-03-17
    • 2013-11-15
    相关资源
    最近更新 更多