WebFaction
Community site: login faq

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

asked 28 Mar '11, 09:08

cha
111
accept rate: 0%


You will need to modify your views to add the additional functionality. If you are familiar with python development, you should be able to follow the Django documentation to make the modifications. If you have a specific question regarding your code, please post your code and we can review it. You can also find a lot of information in the Django user's group here:

https://groups.google.com/forum/#!forum/django-users

permanent link

answered 28 Mar '11, 09:42

aaronh ♦♦
1.3k3
accept rate: 34%

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})

permanent link

answered 31 Mar '11, 08:39

cha
111
accept rate: 0%

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 How i can get it ?

permanent link

answered 01 Apr '11, 03:04

cha
111
accept rate: 0%

I get the Help form django-user

and now I need a little help

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

    {% for choice in poll.choice_set.all %} <li{{ choice.votes="" }}="" --="" {{percentage}}<="" li=""> {% endfor %}


total {{total}}

but the problem with result its will be like this

* 601 -- {1: 0, 2: 0}
* 71 -- {1: 0, 2: 0}

i want the result will be like this

  • 77%
  • 33%
permanent link

answered 03 Apr '11, 08:28

cha
111
accept rate: 0%

You should first fix your percentage calculation - it is incorrect. In order to correctly preserve fractional values during division, you should use something like:

vote_percentage = int(choice.votes*100.0/total_poll_votes)

Then you will need to make the data easily accessible:

percentage[choice] = vote_percentage

And finally you need to correct your template:

{{percentage.choice}}

I would suggest that you go through the Python basics before you jump to Django. One of the best books on Python can be found for free at http://diveintopython.org/

(03 Apr '11, 09:01) tie
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×12
×9

question asked: 28 Mar '11, 09:08

question was seen: 3,686 times

last updated: 03 Apr '11, 09:03

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM