Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:13:57+00:00 2026-06-08T14:13:57+00:00

So I brought the django-polls tutorial into a simple blog. Some posts have polls

  • 0

So I brought the django-polls tutorial into a simple blog. Some posts have polls linked to them through a Foreign Key. The problem I’m having is when I “Vote” or click “Vote again” It re-loads the post but it re-loads using the id for the poll.

Example:

post1 - linked to - poll1
post2 - no poll
post3 - linked to - poll2

so when I vote or click vote again on the poll in post3 it loads post2.

I need to get the id of the post and the poll.

Here is my code:
models.py:

class Post(models.Model):
    title = models.CharField(max_length=60)
    description = models.CharField(max_length=200)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

    def display_mySafeField(self):
        return mark_safe(self.body)

    def __unicode__(self):
        return self.title

class Poll(models.Model):
    question = models.CharField(max_length=200)
    total_votes = models.DecimalField(default=0.0, max_digits=5, decimal_places=2)
    post = models.ForeignKey(Post)
    voted = models.BooleanField(default=False)

    def __unicode__(self):
        return self.question


# Choice for the poll
class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.DecimalField(default=0.0, max_digits=5, decimal_places=2)
    percentage = models.DecimalField(default=0.0, max_digits=5, decimal_places=2)

    def __unicode__(self):
        return self.choice

views.py:

def vote(request, poll_id):
    global choice
    p = get_object_or_404(Poll, pk=poll_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the poll voting form.
        return render_to_response('post.html', {
            'poll': p,
            'error_message': "You didn't select a choice.",
            }, context_instance=RequestContext(request))
    else:
        selected_choice.votes += 1
        p.total_votes += 1
        selected_choice.save()
        p.voted = True
        p.save()

        choices = list(p.choice_set.all())
        for choice in choices:
            percent = choice.votes*100/p.total_votes
            choice.percentage = percent
            choice.save()

        return HttpResponseRedirect(reverse("blog.views.post", args=[poll_id]))

def vote_again(request, post_pk):
    try:
        p = get_object_or_404(Poll, pk=post_pk)
    except (KeyError, Poll.DoesNotExist):
    # Redisplay the poll voting form.
        return render_to_response('post.html', {
        'poll': p,
        'error_message': "You didn't select a choice.",
        }, context_instance=RequestContext(request))
    else:
        p.voted = False
        p.save()
    return HttpResponseRedirect(reverse("blog.views.post", args=[post_pk]))

urls.py:

url(r'^revote/(\d+)/$', 'blog.views.vote_again'),
url(r'^polls/(?P<poll_id>\d+)/vote/$', 'blog.views.vote'),

It all works on the first post and poll, but it crashes when I want to add polls to posts that dont have the same pk.

Any help or direction will be appreciated. I’ve been messing around for about 2 hours and this is the first time django has frustrated me. Apologies in advance because I’m 99% sure this is a stupid question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:13:58+00:00Added an answer on June 8, 2026 at 2:13 pm

    pk means primary key. So there you’re trying to get a poll object whose id is same to the post_pk.

    Try,

    p = get_object_or_404(Poll, post_id=post_pk)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Django 1.2 has brought in some changes in the syndication framework. According to this
I'm slowly getting into the position where one of my Django sites needs some
I have a custom UITableView populated by JSON data brought into a NSMutableDictionary. I
I have several instances where a form is brought into the page in an
I have a plist file that is brought into a tableview, and now want
Importing a Maven project into Eclipse has brought up the error: Plugin execution not
I have a client who has brought in another developer to work on a
A student in my class brought this to my attention, and I didn't have
I have a form that is brought up in a ajaxed modal which I
I was brought aware of this issue by some users on my website. A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.