我无法在论坛上将帖子按各自的类别列出。当我不指定类别时,我可以将帖子列出,但是当我尝试指定类别时,则不会显示帖子。例如,{% for post in posts|slice:":4" %}
将列出4个最新帖子。而{% for post in gen_discussion|slice:":4" %}
什么也没显示。
任何人都可以根据我的以下代码提出解决方案的建议吗?
论坛HTML:
<section class="container">
<div class="my-3 p-3 bg-white rounded shadow-sm">
<h5 class="border-bottom border-gray pb-2 mb-0 forum-category-header">
Recent Posts
</h5>
<!-- A list of recent posts will display here. Limited to 4 most recent posts -->
{% for post in posts|slice:":4" %}
<div class="media text-muted pt-3">
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<strong class="d-block text-gray-dark margin-bottom-5"
><a href="{% url 'forum_post_details' post.id %}"
>{{ post.title }}</a
></strong
>
{{ post.content|truncatewords:30 }}
</p>
<hr />
</div>
{% endfor %}
</div>
<div class="my-3 p-3 bg-white rounded shadow-sm">
<h5 class="border-bottom border-gray pb-2 mb-0 forum-category-header">
General Discussion
</h5>
<!-- A list of recent posts in 'General Discussion' will display here. Limited to 4 most recent posts -->
{% for post in gen_discussion|slice:":4" %}
<div class="media text-muted pt-3">
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<strong class="d-block text-gray-dark margin-bottom-5"
><a href="{% url 'forum_post_details' post.id %}"
>{{ post.title }}</a
></strong
>
{{ post.content|truncatewords:30 }}
</p>
<hr />
</div>
{% endfor %}
<div class="media text-muted pt-3">
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<!-- User can see all 'General Discussion' posts on a new page -->
<a class="view-more-option" href="{% url 'gen_discussion' %}"
>More General Discussion </a
><i class="fas fa-arrow-right"></i>
</p>
</div>
</div>
<div class="my-3 p-3 bg-white rounded shadow-sm">
<h5 class="border-bottom border-gray pb-2 mb-0 forum-category-header">
Events
</h5>
<!-- A list of recent posts in 'Events' will display here. Limited to 4 most recent posts -->
{% for post in events|slice:":4" %}
<div class="media text-muted pt-3">
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<strong class="d-block text-gray-dark"
><a href="{% url 'forum_post_details' post.id %}"
>{{post.title }}</a
></strong
>
{{ post.content|truncatewords:30 }}
</p>
<hr />
</div>
{% endfor %}
<div class="media text-muted pt-3">
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<!-- User can see all 'Events' posts on a new page -->
<a class="view-more-option" href="{% url 'events' %}">More Events </a
><i class="fas fa-arrow-right"></i>
</p>
</div>
</div>
</section>
论坛视图:
@login_required
def get_forum(request):
"""
Will return a list of posts that have been published
and render them to the 'forum.html' template
"""
posts = Post.objects.filter(published_date__lte=timezone.now()
).order_by('-published_date')
gen_discussion = Post.objects.filter(category='General Discussion')
events = Post.objects.filter(category='Events')
return render(request, 'forum.html', {'posts': posts},
{'events': events},
{'gen_discussion': gen_discussion})
答案 0 :(得分:0)
将您的名字更改为
DocumentFile