lciamp
lciamp

Reputation: 685

Return more than one id in django

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.

Upvotes: 0

Views: 289

Answers (1)

Babu
Babu

Reputation: 2598

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)

Upvotes: 1

Related Questions