1
0

vault backup: 2025-12-21 20:21:58
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m30s

This commit is contained in:
2025-12-21 20:21:58 +01:00
parent ec61b89af6
commit 2ec904d899
132 changed files with 1283 additions and 233 deletions

67
quiz/quiz/views.py Normal file
View File

@@ -0,0 +1,67 @@
from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from .models import Question, QuizResult
def index(request):
total_questions = Question.objects.count()
answered_count = QuizResult.objects.filter(user=request.user).count()
context = {
'total_questions': total_questions,
'answered_count': answered_count,
}
return render(request, 'index.html', context)
def get_next_question(request):
answered_ids = QuizResult.objects.filter(user=request.user).values_list('question_id', flat=True)
next_question = Question.objects.exclude(id__in=answered_ids).first()
if not next_question:
return render(request, 'partials/complete.html')
return render(request, 'partials/question.html', {'question': next_question})
@require_http_methods(["POST"])
def submit_answer(request):
question_id = request.POST.get('question_id')
selected_answer = request.POST.get('answer')
if not question_id or not selected_answer:
return HttpResponse("Invalid submission", status=400)
try:
question = Question.objects.get(id=question_id)
except Question.DoesNotExist:
return HttpResponse("Question not found", status=404)
is_correct = selected_answer == question.correct_answer
QuizResult.objects.update_or_create(
user=request.user,
question=question,
defaults={
'selected_answer': selected_answer,
'is_correct': is_correct,
}
)
return get_next_question(request)
def stats(request):
results = QuizResult.objects.filter(user=request.user)
total = results.count()
correct = results.filter(is_correct=True).count()
context = {
'total': total,
'correct': correct,
'percentage': round((correct / total * 100) if total > 0 else 0, 1),
}
return render(request, 'stats.html', context)