I want Develop django poll app tuturial

94 views
Skip to first unread message

cha

unread,
Mar 28, 2011, 12:22:25 PM3/28/11
to django...@googlegroups.com

Hello I Written the Poll Applicattion From the tuturial in django offical website But How i can Develope It ?

I want add bar graph for result and need calculate percentages for vote

cha

unread,
Mar 31, 2011, 2:33:16 AM3/31/11
to django...@googlegroups.com
ok in beginning
how i can get percentage for each vote ?

this result code
def results(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('polls/results.html', {'poll': p})


Sithembewena Lloyd Dube

unread,
Mar 31, 2011, 10:50:38 AM3/31/11
to django...@googlegroups.com, cha
Hi there,

I think that you would need to get the count of votes for each poll, add all votes and use the formula:

vote_percentage = (poll_votes/total_poll_votes)*100



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Regards,
Sithembewena Lloyd Dube

Sithembewena Lloyd Dube

unread,
Mar 31, 2011, 10:51:24 AM3/31/11
to django...@googlegroups.com, cha
^^ This would apply per poll.

cha

unread,
Mar 31, 2011, 12:58:23 PM3/31/11
to django...@googlegroups.com, cha
thanx

I have Reached the following

total_poll_votes = sum(c.votes for c in p.choice_set.all())
poll_votes = ??
vote_percentage = (poll_votes/total_poll_votes)*100

i just need poll_votes

cha

unread,
Apr 1, 2011, 4:00:00 AM4/1/11
to django...@googlegroups.com, cha
Im waiting

Robbington

unread,
Apr 1, 2011, 4:28:53 AM4/1/11
to Django users
Wow thats rude,

poll_votes = the amount of votes for option a/b/c/d etc.

On Apr 1, 9:00 am, cha <mohamma...@gmail.com> wrote:
> Im waiting

Kenneth Gonsalves

unread,
Apr 1, 2011, 5:52:18 AM4/1/11
to django...@googlegroups.com
On Fri, 2011-04-01 at 01:00 -0700, cha wrote:
> Im waiting

for what?
--
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

cha

unread,
Apr 1, 2011, 7:39:34 AM4/1/11
to django...@googlegroups.com

ok .. i know it's the amount of votes for option
how can I get it in python

creecode

unread,
Apr 1, 2011, 11:56:33 AM4/1/11
to Django users
April Fools joke right? :-) HA HA LOL!

On Apr 1, 1:00 am, cha <mohamma...@gmail.com> wrote:

> Im waiting

Toodle-loooooooo..........
creecode

gladys

unread,
Apr 1, 2011, 12:00:48 PM4/1/11
to Django users
For easy reference, models are pasted below.

from django.db import models

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()


# views.py
def results(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
choices = p.choice_set.all()
total_poll_votes = sum(c.votes for c in choices) # total votes
for all choices in this poll
# If i get it correctly, you want the percentage of votes for each
choice?
percentage = {}
for choice in choices:
vote = choice.votes
vote_percentage = (vote/total_poll_votes)*100
# then create a mapping here
percentage[choice.id] = vote_percentage

As the end result, percentage will look like this:
{'1':23, '2':35}


--
Gladys
http://bixly.com

cha

unread,
Apr 1, 2011, 4:24:14 PM4/1/11
to django...@googlegroups.com
thank you very much mr-gladys for this effort


this is view
def results(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    choices = p.choice_set.all()
    total_poll_votes = sum(c.votes for c in choices)
    percentage = {}
    for choice in choices:
        vote = choice.votes
        vote_percentage = int((choice.votes)/(total_poll_votes)) * 100

        # then create a mapping here
        percentage[choice.id] = vote_percentage
    return render_to_response('polls/results.html', {'poll': p,'total':total_poll_votes , 'percentage':percentage},
        context_instance=RequestContext(request))
--------
and this result.html template

<ul>
{% for choice in poll.choice_set.all %}
    <li{{ choice.votes }} -- {{percentage}}</li>
{% endfor %}
</ul>
<br>
total {{total}}

but the problem with result its will be like this
  • 601 -- {1: 0, 2: 0}
  • 71 -- {1: 0, 2: 0}

Reply all
Reply to author
Forward
0 new messages