无法从我的模板中的 BlogPost 对象进行迭代 (Django)
Can't iterate from BlogPost object in my template (Django)
我无法在我的模板中循环访问 BlogPost 对象。由于某种原因没有显示任何内容。我可能忘记了什么。在 shell 中,我可以毫无问题地获取对象。所以出了点问题,我不知道是什么。
views.py:
def latest_posts(request):
latest_posts = BlogPost.objects.all().filter(site_id=1)[:50]
render(request, (settings.PROJECT_ROOT + "/main/templates/includes/latest_posts.html"), {"latest_posts": latest_posts})
latest_posts.html:
{% load pages_tags mezzanine_tags i18n accounts_tags %}
<div class="panel panel-default" >
<div class="panel-heading">
<h3 class="panel-title">{% trans "Latest Posts" %}</h3>
</div>
<div class="panel-body" style="padding:0;border:0px;">
{% for lp in latest_posts %}
<ul class="list-group-latest-posts">
<li class="list-group-item-latest-posts">
<img class="media-object left" src="#" width="40" height="40" alt="#">
<p>{{ lp.title }}<br><span class="latest-post-name">user_name</span><span class="latest-post-divider"> - </span><span class="latest-post-time">6 Hours Ago</span></p>
</li>
</ul>
{% endfor %}
</div>
</div>
这是我的结构。在 base.html:
{% if '/' in request.path %}
{% else %}
{% include "includes/sidebar.html" %}
{% endif %}
sidebar.html:
<div class="col-md-4 right">
{% include 'includes/latest_posts.html' %}
</div>
在我的urls.py:
url("^$", direct_to_template, {"template": "index.html"}, name="home"),
您的页面是从另一个名为 direct_to_template
的视图加载的,该视图与 latest_posts
视图无关,因此它永远不会找到其上下文数据。
所以现在需要发生两件事中的一件,或者您只是使用来自 latest_posts
re: 上下文数据的代码到另一个视图中并将其包含在该上下文中。或者你做一个url指向那个页面
from views import latest_posts
url("^latest_posts$", latest_posts, name="latest_posts"),
现在这将为您显示来自 url /latest_posts
的帖子,但它可能看起来不太漂亮,可以选择 latest_posts
视图仍然加载 base.html 模板,这将使它看起来更像您期望的,尽管浏览 template inheritance 上的文档可能会有更多帮助
我无法在我的模板中循环访问 BlogPost 对象。由于某种原因没有显示任何内容。我可能忘记了什么。在 shell 中,我可以毫无问题地获取对象。所以出了点问题,我不知道是什么。
views.py:
def latest_posts(request):
latest_posts = BlogPost.objects.all().filter(site_id=1)[:50]
render(request, (settings.PROJECT_ROOT + "/main/templates/includes/latest_posts.html"), {"latest_posts": latest_posts})
latest_posts.html:
{% load pages_tags mezzanine_tags i18n accounts_tags %}
<div class="panel panel-default" >
<div class="panel-heading">
<h3 class="panel-title">{% trans "Latest Posts" %}</h3>
</div>
<div class="panel-body" style="padding:0;border:0px;">
{% for lp in latest_posts %}
<ul class="list-group-latest-posts">
<li class="list-group-item-latest-posts">
<img class="media-object left" src="#" width="40" height="40" alt="#">
<p>{{ lp.title }}<br><span class="latest-post-name">user_name</span><span class="latest-post-divider"> - </span><span class="latest-post-time">6 Hours Ago</span></p>
</li>
</ul>
{% endfor %}
</div>
</div>
这是我的结构。在 base.html:
{% if '/' in request.path %}
{% else %}
{% include "includes/sidebar.html" %}
{% endif %}
sidebar.html:
<div class="col-md-4 right">
{% include 'includes/latest_posts.html' %}
</div>
在我的urls.py:
url("^$", direct_to_template, {"template": "index.html"}, name="home"),
您的页面是从另一个名为 direct_to_template
的视图加载的,该视图与 latest_posts
视图无关,因此它永远不会找到其上下文数据。
所以现在需要发生两件事中的一件,或者您只是使用来自 latest_posts
re: 上下文数据的代码到另一个视图中并将其包含在该上下文中。或者你做一个url指向那个页面
from views import latest_posts
url("^latest_posts$", latest_posts, name="latest_posts"),
现在这将为您显示来自 url /latest_posts
的帖子,但它可能看起来不太漂亮,可以选择 latest_posts
视图仍然加载 base.html 模板,这将使它看起来更像您期望的,尽管浏览 template inheritance 上的文档可能会有更多帮助