Django - создание тестирования с несколькими вариантами ответов
Помогите пожалуйста с выводом результатов тестирования. Что сделал:
models:
class Questions(models.Model):
title = models.CharField(max_length=300)
def __str__(self):
return self.title
class Answers(models.Model):
questions = models.ForeignKey(Questions,
on_delete=models.CASCADE,
related_name='questions_list')
title = models.CharField(max_length=300)
correct = models.BooleanField()
def __str__(self):
return self.title
views:
def index(request):
"""Вывод вопросов и вариантов ответов."""
if request.method == "GET":
questions = Questions.objects.all()
context = {
'title': 'Тестирование',
'questions': questions,
}
return render(request, 'tuberculosis_control_day/index.html', context=context)
else:
questions = Questions.objects.all()
answers_list = request.POST.getlist('answers_list')
context = {
'title': 'Подсчет результатов',
'questions': questions,
'answers_list': answers_list
}
return render(request, 'tuberculosis_control_day/index.html', context=context)
template:
{% if request.method == "GET" %}
<form action="." method="POST">
{% csrf_token %}
{% for question in questions %}
<p>{{ question }}:</p>
{% for answers in question.questions_list.all %}
<div>
<input type="checkbox" id="{{ answers.pk }}" name="answers_list" value="{{ answers.pk }}">
<label for="{{ question }}">{{ answers }}</label>
</div>
{% endfor %}
{% endfor %}
<button type="submit">Посмотреть результат</button>
</form>
{% elif request.method == "POST" %}
<h2>Результат</h2>
{% for question in questions %}
<p>{{ question }}:</p>
<div style="color: {% if answers.correct %} green {% else %} red {% endif %}">{{ answers }}</div>
{% endfor %}
Что-то никак не могу понять, как мне сформировать статистику ответов используя answers_list