热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

当有多个可能的选择时,防止重复投票?-Preventduplicatevotingwhenthere'smorethanonepossibleselection?

ImtryingtosetupavotingsysteminusingDjangothatlimitsaregisteredusertovotingonlyon

I'm trying to set up a voting system in using Django that limits a registered user to voting only once on a single vote (despite there being multiple options available agree/strongly agree/disagree). So far, I've been able to set up a system where they can't make the same exact vote (so they can't vote "agree" twice), but they can change their vote and it still goes through (so they can vote "agree" and then vote again as "disagree"). I want them to be limited to one vote per topic, and I can't quite figure out how to tweak my code to accomplish this. Here is my view:

我正在尝试建立一个使用Django的投票系统,限制注册用户在一次投票时只投票一次(尽管有多个选项可用/非常同意/不同意)。到目前为止,我已经能够建立一个他们无法进行同样投票的系统(所以他们不能投票“同意”两次),但是他们可以改变他们的投票并且它仍然可以通过(所以他们可以投票“同意”,然后再次投票为“不同意”)。我希望他们每个主题限制为一票,而我无法弄清楚如何调整我的代码来实现这一目标。这是我的观点:

def vote(request, prediction_id):
    prediction = get_object_or_404(Prediction, pk=prediction_id)
    selected_choice = prediction.choice_set.get(pk=request.POST['choice'])

    if Voter.objects.filter(prediction=prediction, choice=selected_choice, user_id=request.user.id).exists():

        return render(request, 'predictions/detail.html', {
            'prediction': prediction,
            'error_message': "Sorry, but you have already voted."
        })

    else:

        selected_choice.votes += 1
        selected_choice.save()
        Voter.objects.create(prediction=prediction, choice=selected_choice, user_id=request.user.id)

    return HttpResponseRedirect(reverse('predictions:results', args=(prediction.id,)))

and here are my models:

这是我的模特:

class Prediction(models.Model):
    prediction_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    # ...
    def __str__(self):
        return self.prediction_text

class Choice(models.Model):
    prediction = models.ForeignKey(Prediction, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text

class Voter(models.Model):
    user = models.ForeignKey(User)
    choice = models.ForeignKey(Choice)
    prediction = models.ForeignKey(Prediction)

1 个解决方案

#1


0  

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

This line code say if this user have voted in any choice.

此行代码表示此用户是否已选择投票。

If i can understanted, the class Prediction is the "question". So other solution is:

如果我可以理解,那么预测类就是“问题”。所以其他解决方案是:

class Voter(models.Model):
    user = models.ForeignKey(User)
    choice = models.ForeignKey(Choice)
    prediction = models.ForeignKey(Prediction)

Meta:
    unique_together = (("driver", "prediction"),)

推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 十六.增加一个项目协作留言板功能(二)----- 建立一个任务管理的列表页面
    我们设计一个页面来展示正在处理的任务,该表格可以参照之前基础信息的增删改查。用户通过这个页面对任务进行相应操作。1.在views. ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • Django + Ansible 主机管理(有源码)
    本文给大家介绍如何利用DjangoAnsible进行Web项目管理。Django介绍一个可以使Web开发工作愉快并且高效的Web开发框架,能够以最小的代价构建和维护高 ... [详细]
  • python3 logging
    python3logginghttps:docs.python.org3.5librarylogging.html,先3.5是因为我当前的python版本是3.5之所 ... [详细]
  • DRF类视图让你的代码DRY起来
    刚开始写views.py模块的代码,一般都是用def定义的函数视图,不过DRF更推荐使用class定义的类视图,这能让我们的代码更符合DRY(Don'tRepeatYour ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
author-avatar
mobiledu2502900677
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有