Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6681309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:34:31+00:00 2026-05-26T04:34:31+00:00

I have a Django app where users post messages and other users can vote

  • 0

I have a Django app where users post messages and other users can vote the answers up or down, very similar to SO. I’m having an issue trying to get the “thumbs up” and “thumbs down” counts from within the template and I’m hoping someone can help me. The PostVote is a many-to-one relationship with the Post class. Here is what my model looks like:

class Post(models.Model):
    account = models.ForeignKey(Account)
    message = models.CharField(max_length=1024)
    timestamp = models.DateTimeField('post timestamp')

class PostVote(models.Model):
    post = models.ForeignKey(Post)
    account = models.ForeignKey(Account)
    vote = models.CharField(max_length=16, choices=VOTE_CHOICES)
    timestamp = models.DateTimeField('vote timestamp')

Here is how I’m getting my posts:

posts = Post.objects.all().order_by('-timestamp')[:10]

My template looks roughly like:

{% for post in posts %}
<div>Thumbs up count: {{ WHAT_HERE }}</div>
<div>Thumbs down count: {{ WHAT_HERE }}</div>
{% endfor %}

How can I get the counts in there? I’m sure it involves ‘annotate’ somehow, but I’m having a difficult time coming up with this one. Any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:34:32+00:00Added an answer on May 26, 2026 at 4:34 am

    You shouldn’t really be doing logic in your templates. Add a couple count methods to your Post model:

    class Post(models.Model):
        account = models.ForeignKey(Account)
        message = models.CharField(max_length=1024)
        timestamp = models.DateTimeField('post timestamp')
    
        def upvote_count(self):
            return self.postvote_set.filter(vote=VOTE_CHOICES[0][0]).count()
    
        def downvote_count(self):
            return self.postvote_set.filter(vote=VOTE_CHOICES[1][0]).count()
    

    Then use them in your template:

    {% for post in posts %}
    <div>Thumbs up count: {{ post.upvote_count }}</div>
    <div>Thumbs down count: {{ post.downvote_count }}</div>
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form in my django app where users can upload files. How
I have created a django app where users can be looged in with both
I have created a django application. Using this app users can be created or
For my Django app I have Events, Ratings, and Users. Ratings are related to
I have a Django app that uses MySQL as a backend. I'm having difficulties
Let's suppose I have the following url: /valid/django/app/path/?foo=bar&spam=eggs I can simulate a request to
Other posters have previously said in this forum that when your Django app starts
I have a Django app that gets it's data completely from an external source
I have a Django app written in Python 2.5 and I plan to upgrade
I have a Django app that use a django-tagging. I need to port this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.