Created
March 17, 2014 21:28
-
-
Save hcosta/9608755 to your computer and use it in GitHub Desktop.
contador de votos totales
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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