django 2:在模板上呈现单个 post 和建议的 post
django 2: rendering both single post and suggested posts on a template
我正在使用 Django 2,我想在一篇文章的博客页面中同时显示该文章的正文和底部的 3 篇推荐文章。
很遗憾,显示 3 篇文章的部分不起作用。我没有收到任何错误,它只是没有更具体地显示循环中块的任何部分:
我的观点
def detail (request, post_slug):
post = get_object_or_404 (Post, slug=post_slug)
suggested = Post.objects.all()[:3]
return render (request, 'detail.html', {'post':post}, {'suggested':suggested})
和 html 显示建议的
<section class="read-next">
{% for a in suggested.all %}
<a href="/{{a.slug}}" class="prev-post " style="background-image:url({{a.image}})" >
<div class="info">
<div class="tag">We recommend</div>
<h3 class="post-title">{{a.title}}</h3>
</div>
</a>
{% endfor %}
</section> <!-- .read-next -->
此部分或我添加循环的任何地方都不会呈现任何内容。
在此先感谢您的帮助!
当您调用 render
时,您应该 return 单个上下文字典:
return render(request, 'detail.html', {'post':post, 'suggested':suggested})
render
快捷方式的第四个参数是 content_type
,因此您当前的代码相当于:
return render(request, 'detail.html', context={'post':post}, content_type={'suggested':suggested})
这里有几个问题。
render 调用错误。渲染格式如下:
return 渲染(请求,template_name,上下文)
context 是一个字典,可用于在 html 页面上放置变量值。
您正在发送两个单独的词典进行渲染。所以现在对你来说上下文只是一个有一个键的字典:"post"。包含建议的词典设置为 content_type 而不是发送到上下文。
所以你的视图需要变成:
def detail (request, post_slug):
post = get_object_or_404 (Post, slug=post_slug)
suggested = Post.objects.all()[:3]
context = {
"post": post,
"suggested": suggested
}
return render (request, 'detail.html', context)
- 因为您对 Post 个对象 (
suggested = Post.objects.all()[:3]
) 的查询集进行了切片,它现在是一个查询列表,而不是一个可用的查询集。所以你把它当作一个列表来对待。基本上这意味着,你不使用 {% for a in suggested.all %}
因为建议在切片后不再有一个名为 all
的方法。
因此您的模板应使用 {% for a in suggested %}
而不是 {% for a in suggested.all %}
。它之前没有以这种正确方式工作的原因是因为问题 #1 之前建议甚至不在上下文中。
我正在使用 Django 2,我想在一篇文章的博客页面中同时显示该文章的正文和底部的 3 篇推荐文章。
很遗憾,显示 3 篇文章的部分不起作用。我没有收到任何错误,它只是没有更具体地显示循环中块的任何部分:
我的观点
def detail (request, post_slug):
post = get_object_or_404 (Post, slug=post_slug)
suggested = Post.objects.all()[:3]
return render (request, 'detail.html', {'post':post}, {'suggested':suggested})
和 html 显示建议的
<section class="read-next">
{% for a in suggested.all %}
<a href="/{{a.slug}}" class="prev-post " style="background-image:url({{a.image}})" >
<div class="info">
<div class="tag">We recommend</div>
<h3 class="post-title">{{a.title}}</h3>
</div>
</a>
{% endfor %}
</section> <!-- .read-next -->
此部分或我添加循环的任何地方都不会呈现任何内容。 在此先感谢您的帮助!
当您调用 render
时,您应该 return 单个上下文字典:
return render(request, 'detail.html', {'post':post, 'suggested':suggested})
render
快捷方式的第四个参数是 content_type
,因此您当前的代码相当于:
return render(request, 'detail.html', context={'post':post}, content_type={'suggested':suggested})
这里有几个问题。
render 调用错误。渲染格式如下:
return 渲染(请求,template_name,上下文)
context 是一个字典,可用于在 html 页面上放置变量值。
您正在发送两个单独的词典进行渲染。所以现在对你来说上下文只是一个有一个键的字典:"post"。包含建议的词典设置为 content_type 而不是发送到上下文。
所以你的视图需要变成:
def detail (request, post_slug):
post = get_object_or_404 (Post, slug=post_slug)
suggested = Post.objects.all()[:3]
context = {
"post": post,
"suggested": suggested
}
return render (request, 'detail.html', context)
- 因为您对 Post 个对象 (
suggested = Post.objects.all()[:3]
) 的查询集进行了切片,它现在是一个查询列表,而不是一个可用的查询集。所以你把它当作一个列表来对待。基本上这意味着,你不使用{% for a in suggested.all %}
因为建议在切片后不再有一个名为all
的方法。
因此您的模板应使用 {% for a in suggested %}
而不是 {% for a in suggested.all %}
。它之前没有以这种正确方式工作的原因是因为问题 #1 之前建议甚至不在上下文中。