如何在嵌套for循环中为Django数据库查询创建字典?

2024-04-16 11:28:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码

在视图.py在

def render_blog_topics(request, topic):

    categories = get_list_or_404(Category, name=topic)

    """for category in categories:
        for topic in category.topics.all():
            topic_posts += Post.objects.all().filter(topic = topic).order_by('topic')
            print Post.objects.all().filter(topic = topic).order_by('topic').query
            print topic_posts"""
    all_data = []
    all_topics = Topic.objects.all()
    def loop_topic():
        for topic in all_topics:
            current_data = {}
            current_posts = []
            current_posts.append(Post.objects.filter(Q(topic=topic))) 
            # append more posts based on query here like Q(categorie__topic = each_topic) or something
        current_data['topic'] = topic
        current_data['posts'] = current_posts
        all_data.append(current_data)      

    category_posts = Post.objects.filter(category= loop_topic())

    print Post.objects.filter(category= loop_topic())                

    data = {
            'categories': categories,
            'TopicsForm': TopicsForm(),
            'all_data' : all_data

            }
    print all_data 

    return render(request, 'blog_topics.html',data)

基本上,数据变量已经包含的内容用于有关导航元素和站点其他部分的标记。在

我要完成的是

^{pr2}$

我想得到所有的帖子基于当前在循环中的主题,并让它建立一个变量,我可以把它放在数据中。在

例如

主题1--->;与主题1相关的所有帖子 主题2--->;与主题2相关的所有帖子 ……如此这般第四

我该如何构建一个单独的变量来保存主题和与之相关的所有帖子,这样我就可以在模板中循环它了

{% for each topic %}
{% for each post in topic %}

{{ obj.title }}

{% endfor %}
{% endfor %}

(只是想表达我想做什么的逻辑)。在

解决方案

在模板.py在

             {% for each_item in all_data %}
             <div id="{{ each_item.topic }}" class="content_list">
             <a class="title" href="">{{ each_item.topic }}</a>
                <div class="list_container">
                    <ul>
                     {% for posts in each_item.posts %}
                        {% for post in posts %}
                        <li class="python">
                            <a href="">{{ post.title }}<br/> 
                            <span class="date_comments">{{ post.date_created }} | 
                            <span class="comments">12</span></span></a>
                        </li>
                         {% endfor %}
                     {% endfor %}
                    </ul>
                </div>                       
             </div>
             {% endfor %}

在视图.py在

    categories = get_list_or_404(Category, name=topic)           
all_data = []
all_topics = Topic.objects.all()
category_posts = Post.objects.filter(category = categories[0])
print category_posts
for each_topic in all_topics:
    current_data = {}
    current_posts = []
    current_posts.append(category_posts.filter(Q(topic=each_topic))) 
    # append more posts based on query here like Q(categorie__topic = each_topic) or something
    current_data['topic'] = each_topic
    current_data['posts'] = current_posts
    all_data.append(current_data) 
data = {
        'categories': categories,
        'TopicsForm': TopicsForm(),
        'all_data' : all_data

        } 

谢谢


Tags: infordatatopicobjectscurrentallfilter
2条回答

我想你可以这样做。为以下人员申报口述和清单:

topic_dict = {}
topic_list = []

在你的循环里建立字典,比如

^{pr2}$

然后在同一个循环中将这个字典附加到一个列表中,如下所示

topic_list.append(topic_dict)

最后,它会给你一个包含字典的列表,你可以很容易地在模板中循环。在

试试这样。。。在

all_data = []
all_topics = Topic.objects.all()
categorie_posts = Post.objects.filter(categorie=categories)
for each_topic in all_topics:
    current_data = {}
    current_posts = []
    current_posts.append(categorie_posts.filter(Q(topic=each_topic)) 
    # append more posts based on query here like Q(categorie__topic = each_topic) or something
    current_data['topic']=each_topic
    current_data['posts'] = current_posts
    all_data.append(current_data)

现在在模板中

^{pr2}$

相关问题 更多 >