Get test result, but it doesn't work

This commit is contained in:
Piotr Kopycki 2022-01-16 17:51:36 +01:00
parent 3135f3e3ec
commit 9b1143aa41
4 changed files with 51 additions and 19 deletions

View File

@ -262,3 +262,15 @@ background-color:#FF0B7E
.returnLink{
color: #00916E;
}
.testContent input[type=submit]{
height: 30px;
color: #FFF;
font-size: 15px;
background: #00916E;
cursor: pointer;
border-radius: 25px;
border: none;
outline: none;
margin-top: 15px;
}

View File

@ -36,7 +36,7 @@
<div class="list-group">
{% for answer in question.answers.all %}
<label class="list-group-item">
<input class="form-check-input me-1" type="radio" name={{ question.id }} value="">
<input class="form-check-input me-1" type="radio" name={{ question.id }} value={{ answer.id }}>
{{ answer.description }}
</label>
{% endfor %}
@ -44,6 +44,7 @@
{% endfor %}
</div>
<input class="testContent" type="submit" value="Send answers">
</div>
{% endblock %}

View File

@ -7,7 +7,7 @@
<img class="resultImage" src="http://kmit.in/emagazine/wp-content/uploads/2018/02/karnataka-results.jpg" alt="Card image cap">
<div class="resultBody">
<h5 class="resultMsg">Quite good! All the best for next quiz!</h5>
<h5 class="resultScore">Score: 3</h5>
<h5 class="resultScore">Score: {{ request.session.points }}</h5>
<p class="resultText">Percentage: 60%</p>
<p class="resultText">Correct answers: 3</p>

View File

@ -6,6 +6,7 @@ from rest_framework.response import Response
from trials.models import Test
from trials.serializers import TestSerializer
from django.conf import settings
from django.http import HttpResponseRedirect
class TestModelViewSet(viewsets.ModelViewSet):
@ -16,15 +17,33 @@ class TestTemplateView(TemplateView):
permission_classes = []
template_name = settings.BASE_DIR + f"/templates/generic_test.html"
test_id = None
def get_queryset(self):
return Test.objects.all()
def get_context_data(self, test_id, **kwargs):
self.test_id = test_id
context = super().get_context_data(**kwargs)
context["test"] = self.get_queryset().filter(id=test_id).prefetch_related("questions__answers").first()
return context
def post(self, request, *args, **kwargs):
question_id = 1
answers = []
while True:
if request.POST.get(str(question_id)) is None:
break
ans_id = request.POST.get(str(question_id))
answers.append({'question': question_id, 'answer': ans_id})
question_id += 1
points = self.get_queryset().filter(id=self.test_id).get_score(answers)
#return redirect('account')
request.session['points'] = points
url = '/tests/' + str(self.test_id) + '/result'
return HttpResponseRedirect(url)
def testView(request):
permission_classes = []