【问题标题】:Want to show only the first latest post on my main page只想在我的主页上显示第一个最新帖子
【发布时间】:2020-05-19 06:28:44
【问题描述】:

我试图只显示主页上的第一个最新帖子,但仍然循环所有。请问我需要帮助 这是view.py:

def index(request):
    context = {
        'infos': info.objects.all(),
    }
    return render(request, 'blog/index.html',context)

index.html 我想知道如何在从数据库发布时仅显示最新帖子

<div class="img-border">
                <!-- {% for info in infos %} -->
                <a href="{{ infos.video.url }}" class="popup-vimeo image-play">
                  <span class="icon-wrap">
                    <span class="icon icon-play"></span>
                  </span>
                  <img src="{% static 'blog/images/img_2.jpg' %}" alt="" class="img-fluid">
                </a>
              </div>

          </div>
          <div class="col-md-5 ml-auto">

            <span class="caption px-0 text-muted">Latest Sermon</span>
            <h2 class="h2 mb-0">{{ infos.title }}</h2>
            <span class="d-block mb-3"><em>by</em> {{ infos.author }}</span>  
            <p class="h5 mb-2">If ye love Me, keep My Commandments.</p>
            <p class="mb-4 word-wrap1">{{ infos.content }}</p>
            <p><a href="{{ infos.video.url }}" class="popup-vimeo text-uppercase">Watch Video <span class="icon-arrow-right small"></span></a></p>
            <!-- {% endfor %} -->

          </div>
        </div>
      </div>
    </div>

模型.py

class info(models.Model):
    image = models.ImageField(upload_to='profile_pics')
    video = models.FileField(upload_to="videos")
    title = models.CharField(max_length= 100)
    content = models.TextField()
    author = models.CharField(max_length=15)
    date_posted = models.DateTimeField(default = timezone.now)
    published = models.BooleanField(default=True)

【问题讨论】:

标签: python html django


【解决方案1】:

您只需更改您的查询:

def index(request):
context = {
    'info': info.objects.order_by('-date_posted').first(),
}
return render(request, 'blog/index.html', context)

注意:由date_posted 订购是由 Toni Sredanović 在其他答案中提出的,但是 因为它默认为now 它无论如何都会遵循 ID 的顺序所以 可以简化为 info.objects.last()

然后从模板中删除for循环:

<!-- {% for info in infos %} -->

这样的事情现在可以工作了:

{{ info.title }} # things like this will now work

【讨论】:

  • 嘿!你是不是偷了我的回答? :D
  • 我在看到有日期字段时添加了订购。我之前发布的所有其他内容,然后是你 xD。此外,由于日期默认为timezone.now,因此实际上不需要订购 - 无论如何它都会遵循 ID 的顺序。
  • @ToniSredanović 我更新了帖子,所以我们俩都不应该被冒犯
  • 确实,('date_posted').last() 可以使用,但对我来说,理解起来似乎有点复杂(或者只是我曾经在字段上使用减号前缀和fists 调用)
【解决方案2】:
def index(request):
    context = {'infos': info.objects.order_by('-date_posted').first(),}
    return render(request, 'blog/index.html', context)

【讨论】:

    【解决方案3】:

    &lt;!-- ... --&gt;是一个HTML注释,如果你把Django命令放在里面,它们仍然会被执行。尝试改用{% comment %}...{% endcomment %}

    否则,为什么只想显示第一个对象,却要获取所有对象?

    如果我是你,我会这样做:

    def index(request):
        context = {
            'infos': info.objects.filter(published__is=True).order_by('date_posted').first(),
        }
        return render(request, 'blog/index.html',context)
    

    使用这个你当然不需要 for 循环。

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:

      使用info.objects.order_by('-date_posted').first() 代替info.objects.all() 仅获取最新发布的info 对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-11
        • 2018-02-04
        • 1970-01-01
        • 2021-01-27
        • 2014-07-18
        • 2015-05-05
        • 2013-07-27
        • 1970-01-01
        相关资源
        最近更新 更多