Как сравнить ответ пользователя в викторине с правильным ответом в базе данных?

Как я могу сравнить ответ пользователя в тесте с правильным ответом в базе данных и присвоить балл, если ответ правильный?

в настоящее время я получаю все ответы неправильно, после викторины. Я хочу, если пользователь выбрал правильный ответ, чтобы он получил балл, а если ответ неправильный, чтобы он получил 0 баллов.

views.py
def math(request):

    if request.method == 'POST':



        print(request.POST)
        questions = QuesModel.objects.filter(kategorie__exact="mathematics")

        score = 0
        wrong = 0
        correct = 0
        total = 0
        for q in questions:
            total += 1
            answer = request.POST.get(q.question)

            if q.ans == answer:

                score += 10
                correct += 1
            else:
                wrong += 1
        percent = score / (total * 10) * 100
        context = {
            'score': score,
            'time': request.POST.get('timer'),
            'correct': correct,
            'wrong': wrong,
            'percent': percent,
            'total': total,

        }
        return render(request, 'result.html', context)
    else:
        questions = QuesModel.objects.all()
        context = {
            'questions': questions
        }
        return render(request, 'mathe.html', context)
model.py
class QuesModel(models.Model):
    Categorie= (
        ("mathematics", "mathematics"),
        ("Business Administration", "Business Administration"),

    )

    categorie= models.CharField(max_length=400, null=True, choices=Categorie)
    explaination= models.CharField(max_length=400, null=True)


    user = models.ForeignKey(Profile, null=True, on_delete=models.SET_NULL)
    question = models.CharField(max_length=200, null=True)
    op1 = models.CharField(max_length=200, null=True)
    op2 = models.CharField(max_length=200, null=True)
    op3 = models.CharField(max_length=200, null=True)
    op4 = models.CharField(max_length=200, null=True)
    ans = models.CharField(max_length=200, null=True)

    def __str__(self):
        return self.question

math.html

{% extends 'dependencies.html' %}

{% block content %}
{% load static %}
<div class="container ">
<h1>Welcome to DataFlair Quiz</h1>

<div align="right " id="displaytimer"><b>Timer: 0 seconds</b></div>

 <form method='POST' action=''>
    {% csrf_token %}
    {% for q  in questions%}
        {% if q.kategorie == "Mathematik Grundlagen I" %}
        <div class="form-group">
          <label for="question">{{q.question}}</label>
        </div>
        <div class="form-check">
            <div class="form-check">
                <input class="form-check-input" type="radio" name="{{q.question}}" id="gridRadios1" value="option1" checked>
                <label class="form-check-label" for="gridRadios1">
                    {{q.op1}}
                </label>
            </div>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="{{q.question}}" id="gridRadios2" value="option2">
                <label class="form-check-label" for="gridRadios2">
                    {{q.op2}}
                </label>
            </div>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="{{q.question}}" id="gridRadios1" value="option3">
                <label class="form-check-label" for="gridRadios1">
                    {{q.op3}}
                </label>
            </div>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="{{q.question}}" id="gridRadios2" value="option4">
                <label class="form-check-label" for="gridRadios2">
                    {{q.op4}}
                </label>
            </div>
            <br>
        </div>

        {% endif %}
    {% endfor %}
    <input id='timer' type='hidden' name="timer" value="">
    <br>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>
    {% block script %}
        <script>

            console.log('hello world')
            const timer=document.getElementById('displaytimer')
            console.log(timer.textContent)
            const inputtag = document.getElementById('timer')

            t=0
            setInterval(()=>{
                t+=1
                timer.innerHTML ="<b>Timer: " +t+" seconds</b>"
                inputtag.value = t
            },1000)
        </script>
    {% endblock script %}

</div>
{% endblock %}

Было бы здорово, если бы кто-нибудь смог мне помочь

Вернуться на верх