Note that there are some explanatory texts on larger screens.

plurals
  1. POhow do i create a dictionary within a nested for loop for django database query?
    text
    copied!<p>I have the following code;</p> <h1>views.py</h1> <pre><code>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) </code></pre> <p>Basically what the data var already contains is used for tags concerning navigation elemnts and other parts of the site.</p> <p>What i am trying to accomplish is for each topic in</p> <pre><code> for category in categories: for topic in category.topics.all(): print topic </code></pre> <p>I want to get all the posts based off the topic currently in the loop and have it build a variable that i can place in data.</p> <p>For example</p> <p>Topic1 ---> All posts related to topic1 Topic2 ---> ALl posts related to topic2 .....so on so fourth</p> <p>How do i go about building a single varaible that will hold the topic and all the posts associated with it so that i can just loop through it in the template;</p> <pre><code>{% for each topic %} {% for each post in topic %} {{ obj.title }} {% endfor %} {% endfor %} </code></pre> <p>(just trying to convey the logic of what im trying to do).</p> <h1>solution</h1> <p>templating.py</p> <pre><code> {% for each_item in all_data %} &lt;div id="{{ each_item.topic }}" class="content_list"&gt; &lt;a class="title" href=""&gt;{{ each_item.topic }}&lt;/a&gt; &lt;div class="list_container"&gt; &lt;ul&gt; {% for posts in each_item.posts %} {% for post in posts %} &lt;li class="python"&gt; &lt;a href=""&gt;{{ post.title }}&lt;br/&gt; &lt;span class="date_comments"&gt;{{ post.date_created }} | &lt;span class="comments"&gt;12&lt;/span&gt;&lt;/span&gt;&lt;/a&gt; &lt;/li&gt; {% endfor %} {% endfor %} &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; {% endfor %} </code></pre> <p>views.py</p> <pre><code> 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 } </code></pre> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload