Ok, I tried it and it works.
The idea is to put a cookie in the computer of the other person, where
it stocks all the polls it has participated to. That way it does not
depend of the IP.
The code:
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
voted_polls=request.session.get('has_voted_poll',[])
if poll_id in voted_polls:
return render_to_response('sysvortex/polls/poll_results.html', {
'object': p,
'error_message': "You have already voted.",
})
if p.is_open == False :
# Display the poll results.
return render_to_response('sysvortex/polls/poll_results.html', {
'object': p,
'error_message': "The poll is closed.",
})
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form.
return render_to_response('sysvortex/polls/poll_detail.html', {
'object': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
request.session['has_voted_poll']=voted_polls+[poll_id]
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect('/polls/%s/results/' % p.id)
Any comment and improvement welcomed.
Enjoy,
G
On 6/26/06, bahund <[EMAIL PROTECTED]> wrote:
>
> I'm in the same boat as well, where I'll have logged in users as well
> as anonymous users, and each user will only be allowed to submit each
> poll once. I haven't figured out how to handle the anonymous users yet
> (I'll stay tuned to this thread), but I may be able to suggest a way to
> handle it in the model.
>
> What I did was create an additional model for submitted polls. It has:
>
> class SubmittedPoll(models.Model):
> poll = models.ForeignKey(Poll)
> user = models.ForeignKey(User)
> choice = models.ForeignKey(Choice)
> submit_date = models.DateTimeField()
>
> As I said, I still have not been able to handle the anonymous user, so
> I'm hoping someone can shed some light on a way to do with with
> sessions/cookies.
>
> Thanks,
> Andy
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---