首页 > 解决方案 > 我尝试在 django 中构建的测验 - 如何使其工作?

问题描述

我正在构建一个练习希伯来语-英语单词的测验。每次计算机从数据库中提取 4 个单词 rednomiles 并且用户从单词中选择适当的单词。总是从 request.post.get = None 开始的问题。并且缩进是在 None 前面进行的,这意味着即使答案正确,结果也不正确。我该如何解决?

事实上,甚至在用户做出选择之前就已经进行了比较。而且选择后得到的结果通常是错误的。

例如-在第一轮(正确的工作和下一轮将是y)计算机选择x-现在我单击x结果=不正确的x!= y..

下一轮计算机选择 y ,现在我点击 y (正确的工作和下一轮将是 z) 结果 = 不正确的 y != z..

def practice(request):
words_list = Words.objects.filter(approved= True)  # Take all the words that approved by the admin
four_options = [random.choice(words_list) for i in range(4)]
x = four_options[0]
z = x.Hebrew_word
random.shuffle(four_options)
word = z[:4]
choice_user = request.POST.get("quiz")
choice_1 = str(choice_user)
choice = choice_1[:4]

if request.method == "POST":
    if choice == word:
        message = f'Correct {choice} == {word}'
        return render(request, "practice.html",
                      {"four_options": four_options, "x": x, "z": z, "choice": choice, 'word': word,"message":message})
    if choice != word:
       message = f'Incorrect {choice} != {word}'
       return render(request, "practice.html",
                      {"four_options": four_options, "x": x, "z": z,  "choice": choice, 'word': word,"message":message})

return render(request, "practice.html", {"four_options": four_options, "z": z, "x": x,"choice":choice ,'word':word})




<div id="word"> {{x.English_word}}</div>



{%for i in four_options%}
<form method="POST">
        {%csrf_token%}

<br/>
<input name = "quiz" type="submit" value={{i.Hebrew_word}} class="btn btn-primary"/>
    </form>
    {%endfor%}

{{message}}

标签: pythondjango

解决方案


推荐阅读