允许用户投票一次。 Django的 [英] Allow a user to vote only once. Django

查看:114
本文介绍了允许用户投票一次。 Django的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 models.py 中有:

 投票(models.Model):
question = models.CharField(max_length = 200)
pub_date = models.DateTimeField('date published')
is_active = models.BooleanField(default = True)
#其余的代码...

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField max_length = 200)
vote = models.IntegerField(default = 0)
#其余的代码...

class Voter(models.Model):
user = models.ForeignKey(User)
poll = models.ForeignKey(Poll)

什么我在我的投票视图中 views.py

  @login_required 
def vote(request,poll_id):
#一些代码...
#这里是检查发生。
vot = [user.id for user in Voter.objects.filter(poll__id = poll_id)]
if request.user.id在选民中:
return render(request,'polls / detail
})
try:
selected_choice = p.choice_set.get(pk = request.POST ['choice'])
except(KeyError,Choice.DoesNotExist):
#重新显示投票表决。
return render(request,'polls / detail.html',{
'poll':p,
'error_message':你没有选择选项
})
else:
selected_choice.votes + = 1
selected_choice.save()
v = Voter(user = request.user,poll = p)
v。 save()
return HttpResponseRedirect(reverse('polls:results',args =(p.id,)))

但是这段代码有点奇怪。



我已经创建了5个轮询来进行测试。 这段代码只适用于一个
只有在其中一个民意调查中,当我尝试投票两次时,我会收到错误消息。
在这个民意调查的其余部分,代码给了机会投票多次,你想要的。



我不知道为什么。你有什么想法吗?

解决方案

选民适合您的用户 ids。这就解释了为什么你看到它有时似乎工作(当选民ID和用户标识意外匹配时)。替换:

  vot = [user.id for Voter.objects.filter(poll__id = poll_id)] 
如果request.user.id在选民中:


$ b $如果Voter.objects.filter(poll_id = poll_id,user_id = request.user.id).exists()
  p $ p> 

更好的做法是让数据库后端为您进行支票。


What I have in my models.py:

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    is_active = models.BooleanField(default=True)
    # The rest of code...

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    # The rest of code...

class Voter(models.Model):
    user = models.ForeignKey(User)
    poll = models.ForeignKey(Poll)

What I have in my vote view in views.py:

@login_required
def vote(request, poll_id):
    # Some code...
    # And here is the checking happens.
    voters = [user.id for user in Voter.objects.filter(poll__id=poll_id)]
    if request.user.id in voters:
        return render(request, 'polls/detail.html', {
        'poll': p,
        'error_message': "Sorry, but you have already voted."
        })
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the poll voting form.
        return render(request, 'polls/detail.html', {
        'poll': p,
        'error_message': "You didn't select a choice."
        })
    else:
        selected_choice.votes +=1
        selected_choice.save()
        v = Voter(user=request.user, poll=p)
        v.save()
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))

But this code works a little bit strange.

I have created 5 polls for testing purposes. And this code works only for one of them. Only in one of this polls I get the error message when I try to vote twice. In the rest of this polls the code gives the opportunity to vote as many times as you want.

And I have no idea why. Do you have any ideas?

解决方案

Voter ids won't necessarily fit your User ids. This explains why you see it sometimes appear to work (when voter id and user id accidentally match up). Replace:

voters = [user.id for user in Voter.objects.filter(poll__id=poll_id)]
if request.user.id in voters:

with

if Voter.objects.filter(poll_id=poll_id, user_id=request.user.id).exists()

It is better practice to let the database backend do the checks for you.

这篇关于允许用户投票一次。 Django的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆