繁体   English   中英

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

[英]Why isn't my Model Query set working properly?

我正在建立一个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

我不确定问题到底是什么,但我可以通过更改模板过滤器使其仅对queryset起作用,并在调用过滤器时在模板中使用更多for循环来使其正常工作。 我猜想过滤器在这方面有一些限制。 我将尝试找到更具体的要点来阐明此问题并更新此帖子,但在此之前,感谢6位尝试回答我的问题的人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM