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

  • Home
  • SEARCH
  • 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 8360567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:30:02+00:00 2026-06-09T11:30:02+00:00

I made a simple blog and the django polls tutorial. Im trying to get

  • 0

I made a simple blog and the django polls tutorial. Im trying to get them to work together. When I load a post, the poll associated with it loads, the Vote again works but when I click a choice and then the vote button, it loads the post with the id of the poll id. Im not sure if its my “vote” function in views, my “vote” url, or my template thats messed up? Here is my code:

models.py:

# Post class
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

# Poll for the Post
class Poll(models.Model):
    question = models.CharField(max_length=200)
    total_votes = models.IntegerField(default=0)
    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.IntegerField(default=0)
    percentage = models.DecimalField(default=0.0, max_digits=5, decimal_places=2)

    def __unicode__(self):
        return self.choice

urls.py:

urlpatterns = patterns('',
    ### main/index page
    url(r'^$', 'blog.views.main', name='index'),

    ### url for the post.html
    url(r'^post/(\d+)', 'blog.views.post'),

    ### polls
    url(r'^polls/(\d+)/results/$', 'blog.views.results'),
    url(r'^polls/(\d+)/vote/$', 'blog.views.vote'),
    url(r'^revote/(\d+)/$', 'blog.views.vote_again'),

)

views.py:

# main view for the posts
def main(request):
    posts = Post.objects.all().order_by("-created")
    paginator = Paginator(posts, 5)

    try: page = int(request.GET.get("page", '1'))
    except ValueError: page = 1

    try:
        posts = paginator.page(page)
    except (InvalidPage, EmptyPage):
        posts = paginator.page(paginator.num_pages)
    d = dict(posts=posts, user=request.user,
             post_list=posts.object_list, months=mkmonth_lst())

    return render_to_response("list.html", d)

def post(request, pk):
    post = Post.objects.get(pk=int(pk))
    comments = Comment.objects.filter(post=post)
    try:
        poll = Poll.objects.get(post=post)
    except Poll.DoesNotExist:
        poll = None
    d = dict(post=post, comments=comments, form=CommentForm(), user=request.user,
             months=mkmonth_lst(), poll=poll)
    d.update(csrf(request))
    return render_to_response("post.html", d)


#view to vote on the poll
def vote(request, post_id):
    global choice
    p = get_object_or_404(Poll, pk=post_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=[post_id    ]))

def vote_again(request, post_pk):
    try:
        p = get_object_or_404(Poll, post_id=post_pk)
    except (KeyError, Poll.DoesNotExist):
        pass
    else:
        p.voted = False
        p.save()
    return HttpResponseRedirect(reverse("blog.views.post", args=[post_pk]))

this is what is happening:

post1 - linked to - poll1
post2 - not linked
post3 - linked to - poll2

when I vote on poll2 which is linked to post3, it updates the database for poll2 but it reloads post2 instead of post3.

  • 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-09T11:30:03+00:00Added an answer on June 9, 2026 at 11:30 am

    In your HTML form your action is set to /polls/{{ poll.id }}/vote/. However, it’s looking for the post.pk value, not poll.pk. It uses that value to reload the page after committing the data. That should be your problem right there.

    EDIT

    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=[ p.post.pk ] )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have made a simple php contact form following this tutorial: http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme The big
I made a simple pong game from a tutorial (icode blog)and would like to
I have a simple post AR class/table. Its based on the blog tutorial so
So I have the standard django tutorial polls app and I made a little
I have made simple encryption/decryption method in php that I'm trying to move to
I'm using this simple code: http://ejohn.org/blog/simple-javascript-inheritance/ Using this library, I made this simple class:
I've made simple Qt4 design and I want to make it work with c++
I made simple plugin and i would like to work this plugin only below
last year I made really simple blog system. it allows user to authorize, posting,
So I am teaching myself rails and I am trying to create simple blog

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.