Advertisement
MartinYanchev-99

quiz/views.py

Mar 15th, 2021
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. from django.shortcuts import render
  2. from .models import Quiz
  3. from django.views.generic import ListView
  4. from django.http import JsonResponse
  5. class QuizListView(ListView):
  6.     model = Quiz
  7.     template_name = 'quizes/main.html'
  8. # Create your views here.
  9.  
  10. def quiz_view(request,pk):
  11.     quiz = Quiz.objects.get(pk=pk)
  12.     return render(request,'quizes/quiz.html',{'quiz':quiz})
  13.  
  14. def quiz_data_view(request,pk):
  15.     quiz = Quiz.objects.get(pk=pk)
  16.     questions = []
  17.     for q in quiz.get_questions():
  18.         answers = []
  19.         for a in q.get_answers():
  20.             answers.append(a.text)
  21.         questions.append({str(q): answers})
  22.    
  23.     return JsonResponse(
  24.         {
  25.             'data': questions,
  26.             'time': quiz.time
  27.         }
  28.     )
  29.  
  30.  
  31. def save_quiz_view(request,pk):
  32.     print(request.POST)
  33.     return JsonResponse({
  34.         'text': 'work'
  35.     })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement