Python Forum
DJANGO Looping Through Context Variable with specific data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DJANGO Looping Through Context Variable with specific data
#1
Hey there everybody! I am trying to return the number of threads and posts that belong to that specific forum. The structure is like this: Category -> Forum -> Thread.
Look at the picture attached

I want to query the database for all threads and posts that belong to that Forum and get the count of each.

This is my current code:

Models.py
class Forum(models.Model):
    name = models.CharField(max_length=255)
    description = models.CharField(max_length=255)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='forums')

    def __str__(self):
        return self.name


class Thread(models.Model):
    name = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)
    thread_forum = models.ForeignKey(Forum, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Post(models.Model):
    post_body = models.TextField(blank=True, null=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post_date = models.DateTimeField(auto_now_add=True)
    post_thread = models.ForeignKey(Thread, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.id) + ' | ' + str(self.author)
Views.py
class HomeView(ListView):
    context_object_name = 'name'
    template_name = 'index.html'
    queryset = Category.objects.all()

    def get_context_data(self, *args, **kwargs):
        context = super(HomeView, self).get_context_data(*args, **kwargs)
        return context
index.html
{% for forum in category.forums.all %}
  <div class="container-fluid forum-threads clearfix">
    <div class="forum_threads_count">
      100
    </div>
    <div class="forum_threads_threads">
      <span>Threads</span>
    </div>
  </div>
  <div class="container-fluid forum-posts clearfix">
    <div class="forum_posts_count">100</div>
    <div class="form_posts_text">
    <span>Posts</span>
    </div>
    </div>
   <div class="container-fluid forum-latest-posts clearfix">

{% endfor %}
I wish I could do this inside index.html but I can't:
{{ Thread.filter(forum=forum).count()) }}
Got any ideas for a simple way to incorporate this? Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  context Django Sowmya 0 85 Mar-22-2024, 10:47 AM
Last Post: Sowmya
  Save JSON data to sqlite database on Django Quin 0 2,804 Mar-26-2022, 06:22 PM
Last Post: Quin
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 2,763 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,245 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django - Retrieve form data justantest 0 2,813 May-23-2019, 11:47 AM
Last Post: justantest
  [Python 3] - Extract specific data from a web page using lxml module Takeshio 9 7,021 Aug-25-2018, 08:46 AM
Last Post: leotrubach
  Django - Passing data from view for use client side with JavaScript in a template bountyhuntr 0 3,603 Jun-11-2018, 06:04 AM
Last Post: bountyhuntr
  webscraping - failing to extract specific text from data.gov rontar 2 3,138 May-19-2018, 08:01 AM
Last Post: rontar
  Execute using Html, Save data into Database and Download in CSV in Django --Part 1 Prince_Bhatia 0 3,799 Jan-19-2018, 06:05 AM
Last Post: Prince_Bhatia
  How do I make a Django model from a tab-delimited data file? newbietostuff 0 2,500 Jan-09-2018, 02:44 AM
Last Post: newbietostuff

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020