Django问卷投票异常:正确投票无法计入correct_votes问题排查
问题:Django问卷系统投票逻辑判断错误
开发Django问卷系统时,Question表通过answer字段存储管理员配置的正确答案。投票逻辑要求:
- 若用户选择的选项与
answer匹配,将该选项的correct_votes加1,并打印"correct" - 若不匹配,将该选项的
votes加1,并打印"not correct"
但实际运行时,无论投票是否正确,始终打印"not correct",正确投票无法计入correct_votes,仅触发votes累加。
相关代码
view.py
def vote(request, question_id): question = Question.objects.get(pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'home_tests/question.html', { 'question': question, 'error_message': "You didn't select a choice.", }) else: if selected_choice.votes == question.answer: print('correct') selected_choice.correct_votes += 1 # selected_choice.save() elif selected_choice.votes != question.answer: print('not correct') selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(reverse('home_tests:results', args=(question.id,)))
models.py
class Question(models.Model): objects = None language = models.ForeignKey(Language, null=True, blank=True, on_delete=models.CASCADE) question_text = models.CharField(max_length=200, null=True, blank=True) answer = models.CharField(max_length=250, null=True, blank=True) pub_date = models.DateTimeField('date published', null=True, blank=True) class Choice(models.Model): DoesNotExist = None language = models.ForeignKey(Language, null=True, blank=True, on_delete=models.CASCADE) question = models.ForeignKey(Question, on_delete=models.CASCADE, null=True, blank=True) choice_text = models.CharField(max_length=250, null=True, blank=True) votes = models.IntegerField(default=0, null=True, blank=True) correct_votes = models.IntegerField(default=0, null=True, blank=True) user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
请求日志
Quit the server with CONTROL-C. [09/Jan/2023 18:49:27] "GET /home_tests/home/ HTTP/1.1" 200 2591 [09/Jan/2023 18:49:28] "GET /home_tests/10/ HTTP/1.1" 200 785 not correct [09/Jan/2023 18:49:40] "POST /home_tests/10/vote/ HTTP/1.1" 302 0 [09/Jan/2023 18:49:40] "GET /home_tests/10/results/ HTTP/1.1" 200 521
解决方案
问题出在判断条件的错误使用:
selected_choice.votes是Integer类型的投票计数,而question.answer是CharField类型的正确答案文本- 两者类型不匹配、内容无关联,导致判断永远不成立,只会走else分支
修改vote函数中的判断逻辑,将selected_choice.votes替换为selected_choice.choice_text(选项的文本内容)与question.answer对比:
def vote(request, question_id): question = Question.objects.get(pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'home_tests/question.html', { 'question': question, 'error_message': "You didn't select a choice.", }) else: # 对比选项文本和正确答案 if selected_choice.choice_text == question.answer: print('correct') selected_choice.correct_votes += 1 else: print('not correct') selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(reverse('home_tests:results', args=(question.id,)))
额外优化点:
- 去掉多余的
elif判断,直接用else覆盖不匹配场景 - 原代码中注释的
selected_choice.save()无需单独调用,最后统一保存即可
内容的提问来源于stack exchange,提问作者Darina Martymianova
相关产品推荐
相关产品推荐

