[URL="http://twitpic.com/dyik6w/full"]http://twitpic.com/dyik6w/full[/URL]
La cuestion es que no se como traer el valor de los votos totales, creo que es debido alguna relacion con mis tablas, pero la verdad ando perdido. Este es mi codigo:
models.py
Código:
views.py import datetime from django.utils import timezone from django.db import models # Create your models here class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question def was_pusblished_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1) was_pusblished_recently.admin_order_field = 'pub_date' was_pusblished_recently.boolean = True was_pusblished_recently.short_description = 'Published recently?' class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) class Meta: ordering = ['-votes'] def __unicode__(self): return self.choice_text
Código:
y el templatefrom django.shortcuts import render, get_object_or_404 from django.http import HttpResponse, HttpResponseRedirect from django.core.urlresolvers import reverse from django.views import generic from polls.models import Poll, Choice # Create your views here. class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_poll_list' def get_queryset(self): return Poll.objects.order_by('-pub_date')[:5] class DetailView(generic.DetailView): model = Poll template_name = 'polls/detail.html' class ResultsView(generic.DetailView): model = Poll template_name = 'polls/results.html' def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'polls/detail.html', {'poll':p, 'error_message': 'You didnt select a choice',}) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
Código:
alguien sabe como puedo obtener los votos totales ??? <h1>{{ poll.question }}</h1> <h4><i>{{ poll.pub_date}}</i></h4> <ul> {% for choice in poll.choice_set.all %} <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li> {% endfor %} </ul> <p>Total de votos {{ choice.votes }}</p> <!--- no se visualiza --> <a href="{% url 'polls:detail' poll.id %}">Vote again?</a>