html模板中的Django for循环未显示

2024-04-23 22:37:03 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我试图得到最新的帖子,我遇到了一个问题,它将不显示该帖子

在视图.py在

def latest_post(request):
    latest = Post.objects.all().order_by('-id')[:1]
    context = {'latestPost': latest}
    return render(request, 'latest_post.html', context)

注意:我也尝试过这个,最新=Post.objects.all()

数据库中有条目,我用shell测试过

^{pr2}$

最新_帖子.html在

{% for newest in latestPost %}
<section class="hero is-primary is-medium">
  <div class="hero-body header">
    <div class="container">
     <div class="font">
      <h1 class="title is-1">
        <span id="blog_title">{{ newest.title }}</span>
      </h1>
      <h2 class="subtitle is-3 subup">
        <span id="blog-subtitle">{{ newest.content|truncatechars:20|safe }}</span>
      </h2>
      <h2 class="subtitle is-5 dateup">
        <span id="blogdate">{{ newest.timestamp }}</span><br><br>
        <a href="{{ newest.blog_url }}" class="button is-danger is-large is-inverted">Read More >></a>
      </h2>
    </div>
    </div>
  </div>
</section>
{% endfor %}

在我的博客里_索引.html我有以下内容

{% extends "blog_base.html" %}
{% block blog_main %}
{% load staticfiles %}

{% include 'latest_post.html' %}
<p> other html here </p>
{% endblock %}

最新_帖子.html使用{%include'最新版本时显示_帖子.html'%}如果我不使用

{% for newest in latestPost %}
{% endfor %}

所以我确信在某些地方没有任何排印错误妨碍最新的_帖子.html从显示在我的索引页。在

我的模型.py在

class Post(models.Model):
    title = models.CharField(max_length=120)
    slug = models.SlugField(unique=True)
    content = models.TextField()
    draft = models.BooleanField(default=False)
    publish = models.DateField(auto_now=False, auto_now_add=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

    def __str__(self):
       return self.title

    def blog_url(self):
        return reverse("blogposts:blogdetail", kwargs={"slug": self.slug})

附加说明:python3,django 1.11,sqlite。 此外,控制台中不会显示错误。任何帮助都将不胜感激!谢谢您!!在


Tags: dividfalseautotitleismodelshtml
2条回答

在前面的代码中,您通过使用[:1]来限制它,因此它只返回一个项。然后你又在一个项目上使用了forloop。不是最好的做事方式。在

Are you trying to get only one item from the post? If yes, Forloop is not needed change your latest_post.html to

<section class="hero is-primary is-medium">
  <div class="hero-body header">
    <div class="container">
     <div class="font">
      <h1 class="title is-1">
        <span id="blog_title">{{ latestPost.title }}</span>
      </h1>
      <h2 class="subtitle is-3 subup">
        <span id="blog-subtitle">{{ latestPost.content|truncatechars:20|safe }}</span>
      </h2>
      <h2 class="subtitle is-5 dateup">
        <span id="blogdate">{{ latestPost.timestamp }}</span><br><br>
        <a href="{{ latestPost.blog_url }}" class="button is-danger is-large is-inverted">Read More >></a>
      </h2>
    </div>
    </div>
  </div>
</section>

您甚至不需要在视图中使用[:1]。有更好的方法

似乎您正在将上下文变量直接传递给latest_post.html

return render(request, 'latest_post.html', context)

但是在blog_index.html中没有这样的上下文变量latestPost。 您需要做的是向blog_index.html添加上下文。将此添加到index视图中:

^{pr2}$

也可以使用first选择queryset中的第一个元素。在

相关问题 更多 >