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!
You shouldn’t really be doing logic in your templates. Add a couple count methods to your
Postmodel:Then use them in your template: