为什么我的模型查询集不能正常工作?

时间:2016-09-17 00:41:13

标签: python django django-models django-templates django-template-filters

我正在构建一个Q& A样式社区,其中的类别包含包含主题的论坛,每个主题都有帖子。在目标网页上,我想要所有类别的热门主题,当在一个类别中时,我想要该类别中的所有热门主题。我已经定义了模板过滤器来执行此操作,但它们不会在类别或主页级别显示任何内容。他们在论坛层面上工作,即在特定论坛内,它能够获取该论坛中的所有主题。在一个类别中,它应该从该类别中的每个论坛获取所有主题,目前,它只返回该类别中的论坛列表而不是所有主题的列表。在家庭层面,它应该返回所有类别和包含论坛的主题列表,但我什么都没得到。

有人可以帮我弄清楚这里发生了什么:?感谢

我的TOPICSFORALL.HTML:

{% block card_body %}
{% for topic in topic_list %}
    {% include "subjectcard.html" with topic=topic %}
{% endfor %}
{% endblock %}

我的SUBJECTCARD.HTML:

{% block card_body %}
{% for topic in topic_list %}
    {% include "subjectcard.html" with topic=topic %}
{% endfor %}
{% endblock %}

我的SITE_BASE.HTML:

{% extends "theme_bootstrap/base.html" %}
{% load i18n pybb_tags topicsbycat topicsbyforum %}
{% load static %}

{% catindexlist as catindexlisted %}
{% topicsbyall as topicsbyallcatnforum %}

...
{% block body %}
    <!-- Main hero unit for a primary marketing message or call to action -->

    <div class="col-xs-2 col-sm-3 col-md-2 col-lg-2" style="border-right:solid;text-align:right;height:99%;padding:0 0 0 0px;" id="sidebar">{% include "categoryindex.html" %}</div>
    <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" id="homebread">{% include "breadcrumbbt.html" %}</div>

    {% if category %}

    <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">{% include "topicstyleforumindex.html" %}</div>
    <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">{% include "topicsforall.html" with topic_list=category|topicsbycat %}</div>

    {% elif forum %}

    <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">{% include "topicindex.html" %}</div>
    <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">{% include "topicsforall.html" with topic_list=forum|topicsbyforum %}</div>

    {% elif topic %}
    <div class="main-block col-xs-6 col-xs-offset-1 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-2 col-lg-6 col-lg-offset-2" id="content" style="height:80%;">{% include 'pybb/topic.html' %}</div>

    {% else %}

    <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">{% include "topicstyleforumindex.html" %}</div>
    <div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 col-lg-10 col-lg-offset-2" style="height:auto;padding-bottom:10px;text-align:center;">{% include "topicsforall.html" with topic_list=topicsbyallcatnforum %}</div>

    {% endif %}

{% endblock %}

我的PYBB_TAGS.PY:

@register.assignment_tag
def topicsbyall():
    topic_list = Topic.objects.all().order_by('-updated', '-created')
    return topic_list

我的TOPICSBYFORUM.PY: register = template.Library()

@ register.filter

def topicsbyforum(forum):
    topic_list = Topic.objects.filter(forum=forum).order_by('post_count','views','-updated')
    return topic_list

我的TOPICSBYCAT.PY:

register = template.Library()

@register.filter
def topicsbycat(category):
    topic_list = []
    catforum = Forum.objects.filter(category=category).order_by('topic_count','post_count')
    for forum in catforum:
        forumtopics = Topic.objects.filter(forum=forum).order_by('post_count','views','-updated')
        topic_list.append(forumtopics)
    return topic_list

1 个答案:

答案 0 :(得分:0)

我不确定究竟是什么问题但是我有点工作,通过更改模板过滤器来执行每个查询集,并在调用过滤器时在模板中使用更多for循环。我想过滤器在这方面有一些限制。我会尝试找到更具体的要点来澄清这个问题并更新这篇文章,但在此之前,感谢6位试图回答我的问题的人!