I'm new to django and I don't understand why the answers to the questions are not displayed on the screen (although the questions themselves are displayed). Everything is also empty in the source code of the page. Help please
model.py
from django.utils.translation import gettext as _
class people(models.Model):
CHOICES_TEST_ID = (('0', 'КСС'),
('1', 'Золотые правила'),
('2', 'MWD'),
('3', 'DD'))
firstname = models.CharField(max_length=50, null=False)
lastname = models.CharField(max_length=50, null=False)
surname = models.CharField(max_length=50, null=False)
test_id = models.CharField(max_length=10, choices=CHOICES_TEST_ID, default=0, blank=False)
date_of_passage = models.DateField(_('Date'), auto_now_add=True)
correct_answer = models.IntegerField(default=0)
done = models.BooleanField('Тест сдан', default=False)
def number_correct_answer(self):
self.correct_answer += 1
class questions(models.Model):
question = models.CharField('Вопрос', max_length=500, null=False)
test_id = models.CharField('Идентификационный номер теста', max_length=2, null=False)
def __str__(self):
return self.question
class Meta:
verbose_name_plural = 'Вопросы'
class answers(models.Model):
answer = models.CharField('Ответ', max_length=200, null=False)
true_answer = models.BooleanField('Правильный ответ', default=False)
question = models.ForeignKey(questions, on_delete=models.CASCADE)
def __str__(self):
return self.answer
class Meta:
verbose_name_plural = 'Ответы'
forms.py
from .models import people, answers
from django.forms import ModelForm, TextInput, RadioSelect
class PeopleForm(ModelForm):
class Meta:
model = people
fields = ['firstname', 'lastname', 'surname', 'test_id']
widgets = {'firstname': TextInput(attrs={'class': 'firstname', 'placeholder': 'Ввод'}),
'lastname': TextInput(attrs={'class': 'lastname', 'placeholder': 'Ввод'}),
'surname': TextInput(attrs={'class': 'surname', 'placeholder': 'Ввод'}),
'test_id': RadioSelect()}
class AnswersForm(ModelForm):
class Meta:
model = answers
fields = ['answer', 'true_answer', 'question']
widgets = {'true_answer': RadioSelect()}
views.py
import random
from django.shortcuts import render, redirect
from .forms import PeopleForm, AnswersForm
from django.core.cache import cache
from .models import questions, people
def first(request):
if request.method == 'POST':
form = PeopleForm(request.POST)
if form.is_valid():
cache.set('test_id', form.cleaned_data.get("test_id"))
form.save()
cache.set('id_people', people.objects.values_list('id', flat=True).last())
return redirect('main')
form = PeopleForm()
data = {'form': form}
return render(request, 'main/first_page.html', data)
def random_questions(test_id):
questions_all = questions.objects.filter(test_id=test_id).values_list('pk', flat=True)
questions_selected = set()
while len(questions_selected) != 3:
questions_selected = questions_selected.union(set(random.sample(sorted(questions_all), 3)))
selected_questions = questions.objects.filter(pk__in=questions_selected, test_id=test_id)
return selected_questions
def main(request):
test_id = cache.get('test_id')
selected_questions = random_questions(test_id)
if request.method == 'POST':
form_main = AnswersForm(request.POST)
if form_main.is_valid():
correct = 0
if form_main.cleaned_data.get('true_answer'):
correct += 1
id_people = cache.get('id_people')
people.objects.filter(pk=id_people).update(correct_answer=correct)
if correct == 3:
return redirect('done')
if correct != 3:
return redirect('lose')
else:
form_main = AnswersForm()
data = {'selected_questions': selected_questions,
'form': form_main}
return render(request, 'main/main_page.html', data)
def end_lose(request):
id_people = cache.get('id_people')
correct_answers = people.objects.filter(pk=id_people).values('correct_answer')
return render(request, 'main/end_page_lose.html', {'correct_answers': correct_answers})
def end_done(request):
id_people = cache.get('id_people')
correct_answers = people.objects.filter(pk=id_people).values('correct_answer')
return render(request, 'main/end_page_successfully.html', {'correct_answers': correct_answers})
main.html
{% extends 'main/base_page.html' %}
{% block title %}
Тестирование
{% endblock %}
{% block article %}
<article>
<form method="post">
{% csrf_token %}
{% for question in selected_questions %}
<h2>{{ question.question }}</h2>
{% if form_main.question_id == question.id %}
{{form_main.answer}}{{form_main.true_answer}}
{% endif %}
{% endfor %}
<button type="submit">Далее</button>
</form>
</article>
{% endblock %}
I searched all similar problems and tried to do it differently. Unfortunately, the result is negative. I am also considering the output of not all questions on one page, but alternately. But I can't figure out how to implement this, since I need unique questions in the amount of 15 out of the total number of questions