Skip to content

Instantly share code, notes, and snippets.

@hcosta
Created March 17, 2014 21:28
Show Gist options
  • Save hcosta/9608755 to your computer and use it in GitHub Desktop.
Save hcosta/9608755 to your computer and use it in GitHub Desktop.
contador de votos totales
# polls/templatetags/poll_extras.py
from django import template
from polls.models import Choice, Poll
register = template.Library()
@register.filter(name='total_votes')
def total_votes(poll):
total = 0 # contador
choices = Choice.objects.filter(poll=poll)
for choice in choices:
total+=choice.votes # vamos sumando los votos
return total
# polls/templates/results.html
{% load poll_extras %}
<h1>{{ poll.question }}</h1>
<h2>Votos totales: {{ poll|total_votes }}</h2>
<ul>
{% for choice in poll.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment