1👍
This is a working solution. It’s not pretty, so I would appreciate ideas to refacture it.
Starting with view.py:
class Plus_im_10erPageView(TemplateView):
template_name = 'plus_im_10er.html'
success_html = 'plus_im_10er_check.html'
no_of_tasks = 3
@classmethod
def generate_plus10_tasks(self):
"""
Generate tasks containing two addends with a sum less or equal 10.
"""
task_list = []
for _ in range(self.no_of_tasks):
while True:
x = random.randint(0, 9)
y = random.randint(0, 9)
result = x + y
if result <= 10:
task_list.append((x, y, result))
break
self.task_list = task_list
return task_list
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) # noqa
context['task_list'] = self.generate_plus10_tasks()
self.context = context
return context
def post(self, request):
form = AnswerForm(request.POST)
answered_tasks = []
if form.is_valid():
answers = form.cleaned_data
total_correct_answers = 0
# helper to get the form_field name
index = 1
for task in self.task_list:
if index > self.no_of_tasks:
break
answer = int(answers[f'answer_{index}'])
x, y, correct_result = task
if answer == correct_result:
total_correct_answers += 1
answered_tasks.append((x, y, correct_result, answer,
total_correct_answers)) # noqa
index += 1
return render(request,
self.success_html,
{'answered_tasks': answered_tasks}
)
The forms:
from django import forms
class AnswerForm(forms.Form):
answer_1 = forms.CharField(label='answer', required=False)
answer_2 = forms.CharField(label='answer', required=False)
answer_3 = forms.CharField(label='answer', required=False)
answer_4 = forms.CharField(label='answer', required=False)
answer_5 = forms.CharField(label='answer', required=False)
answer_6 = forms.CharField(label='answer', required=False)
answer_7 = forms.CharField(label='answer', required=False)
answer_8 = forms.CharField(label='answer', required=False)
answer_9 = forms.CharField(label='answer', required=False)
answer_10 = forms.CharField(label='answer', required=False)
plus_im_10er.html
<!-- templates/plus_im_10er.html -->
{% extends 'base.html' %}
{% block content %}
<h2>Plus rechnen im 10er-Raum</h2>
<ul>
<form method="POST">{% csrf_token %}
{% for task in task_list %}
<li><label for="id_answer">{{ task.0 }} + {{ task.1 }} = </label>
<input id="id_answer" type="text" size=2 name="answer_{{ forloop.counter }}"
onkeypress="return event.charCode >= 48 && event.charCode <=57">{{ form }}
</input>
</li>
{% endfor %}
</ul>
</br></br>
<form action="/plus_im_10er_check/" method="GET">
<input type="submit" value="Antworten prüfen">
</form>
{% endblock content %}
plus_im_10er_check.html
<!-- templates/plus_im_10er_check.html -->
{% extends 'base.html' %}
{% block content %}
<h2>Plus rechnen im 10er-Raum</h2>
<h3>Antworten prüfen</h3>
<ul>
<div>
{% for task in answered_tasks %}
{% if task.2 == task.3 %}
<li>{{ task.0 }} + {{ task.1 }} = {{ task.3 }} 😃</li>
{% else %}
<li>{{ task.0 }} + {{ task.1 }} ≠ {{ task.3 }}</li>
{% endif %}
{% endfor %}
</br>
Du hast {{ answered_tasks.2.4 }} 😃 von 3.
</div>
</br></br>
<a href="/plus_im_10er/"><button type="button">Neue Aufgaben</button></a>
</ul>
{% endblock content %}
Ande the result looks like this:
Source:stackexchange.com