django模板首先显示最新数据

时间:2019-06-04 04:17:38

标签: django

我必须首先在上方显示数据的最新日期,在我的原始代码中,它仅在上方显示数据的最早日期,并在下方显示较新的数据

django 2.2

modle.py

from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse


class Article(models.Model):
    title = models.CharField(max_length=255)
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE,
    )

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('article_detail', args=[str(self.id)])

我认为在views.py中可能是我要做的

模型=文章-> order_by('-date')

类似的东西?我该怎么办?

views.py

from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from django.urls import reverse_lazy

from .models import Article


class ArticleListView(LoginRequiredMixin, ListView):
    model = Article
    template_name = 'article_list.html'
    login_url = 'login'

在表单打击中,它将以向上的顺序列出最旧的数据,而以向下的顺序列出较新的数据,我想做的是向上的最新数据和向下的较旧数据

article_list.html

{% extends 'base.html' %}

{% block title %}Articles{% endblock title %}

{% block content %}
  {% for article in object_list %}
    <div class="card">
      <div class="card-header">
        <span class="font-weight-bold">{{ article.title }}</span> &middot;
        <span class="text-muted">by {{ article.author }} | {{ article.date }}</span>
      </div>
      <div class="card-body">
        {{ article.body }}
      </div>
      <div class="card-footer text-center text-muted">
          <a href="{% url 'article_edit' article.pk %}">Edit</a> |
          <a href="{% url 'article_delete' article.pk %}">Delete</a>
      </div>
    </div>
    <br />
  {% endfor %}
{% endblock content %}

感谢您的审核

3 个答案:

答案 0 :(得分:2)

您可以通过在模型的ordering中设置Meta来做到这一点:

要以相反的顺序显示,请在字段名称前加上 -

class Article(models.Model):
    title = models.CharField(max_length=255)
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE,
    )

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('article_detail', args=[str(self.id)])

    class Meta:
        ordering = ['-date']

答案 1 :(得分:1)

覆盖get_context_data()方法

class ArticleListView(LoginRequiredMixin, ListView):
    model = Article
    template_name = 'article_list.html'
    login_url = 'login'

    def get_context_data(self, *args, **kwargs):
        context_data = super().get_context_data(*args, **kwargs)
        if context_data['object_list']:
            context_data['object_list'] = context_data['object_list'].order_by('-date')
        return context_data

答案 2 :(得分:1)

我认为最简单的方法是使用ordering

class ArticleListView(LoginRequiredMixin, ListView):
    model = Article
    template_name = 'article_list.html'
    login_url = 'login'
    ordering = ['-date']

我建议采用这种方法,因为排序是特定于该ListView的,如果您在模型中进行更改,它将在所有地方反映出来(即使在不需要-date进行排序的地方) )