Pamela Fox
commited on
Commit
·
f85cf8e
1
Parent(s):
1dbd98a
Question display, CSS
Browse files- quizzes/models.py +1 -33
- quizzes/templates/quizzes/display.html +57 -20
- quizzes/templates/quizzes/partial.html +5 -0
- quizzes/urls.py +3 -3
- quizzes/views.py +22 -8
quizzes/models.py
CHANGED
|
@@ -32,17 +32,6 @@ class Question(models.Model):
|
|
| 32 |
def __str__(self):
|
| 33 |
return self.prompt
|
| 34 |
|
| 35 |
-
def display(self):
|
| 36 |
-
print(self.prompt)
|
| 37 |
-
self.answer.display()
|
| 38 |
-
user_answer = input()
|
| 39 |
-
if self.answer.is_correct(user_answer):
|
| 40 |
-
print("You got it!")
|
| 41 |
-
self.answer_status = 'correct'
|
| 42 |
-
else:
|
| 43 |
-
print(f"Sorry, it was: {self.answer.correct_answer}")
|
| 44 |
-
self.answer_status = 'incorrect'
|
| 45 |
-
|
| 46 |
class Answer(models.Model):
|
| 47 |
question = models.OneToOneField(
|
| 48 |
Question,
|
|
@@ -65,12 +54,6 @@ class FreeTextAnswer(Answer):
|
|
| 65 |
return user_answer.lower() == self.correct_answer.lower()
|
| 66 |
return user_answer == self.correct_answer
|
| 67 |
|
| 68 |
-
def display(self):
|
| 69 |
-
if self.case_sensitive:
|
| 70 |
-
print("Type your answer in (capitalization matters!):")
|
| 71 |
-
else:
|
| 72 |
-
print("Type your answer in (don't worry about capitalization):")
|
| 73 |
-
|
| 74 |
class MultipleChoiceAnswer(Answer):
|
| 75 |
choices = fields.ArrayField(models.CharField(max_length=200, blank=True))
|
| 76 |
|
|
@@ -78,19 +61,4 @@ class MultipleChoiceAnswer(Answer):
|
|
| 78 |
return f"{self.correct_answer} from {self.choices}"
|
| 79 |
|
| 80 |
def is_correct(self, user_answer):
|
| 81 |
-
|
| 82 |
-
return self.choices[int(user_answer) - 1] == self.correct_answer
|
| 83 |
-
|
| 84 |
-
def is_correct(self, user_answer):
|
| 85 |
-
self.user_answer = int(user_answer)
|
| 86 |
-
self.user_answer -= 1
|
| 87 |
-
correct_index = self.choices.index(self.correct_answer)
|
| 88 |
-
if self.user_answer == correct_index:
|
| 89 |
-
return True
|
| 90 |
-
else:
|
| 91 |
-
return False
|
| 92 |
-
|
| 93 |
-
def display(self):
|
| 94 |
-
print("Type the number corresponding to the correct answer.")
|
| 95 |
-
for i in range(0, len(self.choices)):
|
| 96 |
-
print(f"{i + 1}. {self.choices[i]}")
|
|
|
|
| 32 |
def __str__(self):
|
| 33 |
return self.prompt
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
class Answer(models.Model):
|
| 36 |
question = models.OneToOneField(
|
| 37 |
Question,
|
|
|
|
| 54 |
return user_answer.lower() == self.correct_answer.lower()
|
| 55 |
return user_answer == self.correct_answer
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
class MultipleChoiceAnswer(Answer):
|
| 58 |
choices = fields.ArrayField(models.CharField(max_length=200, blank=True))
|
| 59 |
|
|
|
|
| 61 |
return f"{self.correct_answer} from {self.choices}"
|
| 62 |
|
| 63 |
def is_correct(self, user_answer):
|
| 64 |
+
return user_answer == self.correct_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
quizzes/templates/quizzes/display.html
CHANGED
|
@@ -2,31 +2,68 @@
|
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8">
|
| 5 |
-
<meta name="viewport" content="width=device-width">
|
| 6 |
-
<title>Quiz {{ quiz.
|
|
|
|
| 7 |
</head>
|
| 8 |
<body>
|
| 9 |
-
<
|
|
|
|
| 10 |
|
| 11 |
-
<
|
|
|
|
| 12 |
{% csrf_token %}
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
</section>
|
| 27 |
-
{% endfor %}
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
</body>
|
| 32 |
</html>
|
|
|
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<title>Quiz: {{ quiz.name }}</title>
|
| 7 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
|
| 8 |
</head>
|
| 9 |
<body>
|
| 10 |
+
<div class="container">
|
| 11 |
+
<h1>Quiz: {{ quiz.name }}</h1>
|
| 12 |
|
| 13 |
+
<section class="bg-warning" style="padding:12px">
|
| 14 |
+
<form id="question-form" action="{% url 'quizzes:grade_question' question.id %}" method="post">
|
| 15 |
{% csrf_token %}
|
| 16 |
+
|
| 17 |
+
<fieldset class="form-group">
|
| 18 |
+
<legend>{{ question.prompt }}</legend>
|
| 19 |
+
{% if question.freetextanswer %}
|
| 20 |
+
<div class="form-group">
|
| 21 |
+
<label for="answer">
|
| 22 |
+
{% if question.freetextanswer.case_sensitive %}
|
| 23 |
+
Type your answer in (capitalization matters!):
|
| 24 |
+
{% else %}
|
| 25 |
+
Type your answer in (don't worry about capitalization):
|
| 26 |
+
{% endif %}
|
| 27 |
+
</label>
|
| 28 |
+
<input type="text" name="answer" class="form-control">
|
| 29 |
+
</div>
|
| 30 |
+
{% else %}
|
| 31 |
+
{% for choice in question.multiplechoiceanswer.choices %}
|
| 32 |
+
<div class="radio">
|
| 33 |
+
<label>
|
| 34 |
+
<input type="radio" name="answer" value="{{ choice }}">
|
| 35 |
+
{{ choice}}
|
| 36 |
+
</label>
|
| 37 |
+
</div>
|
| 38 |
+
{% endfor %}
|
| 39 |
+
{% endif %}
|
| 40 |
+
</fieldset>
|
| 41 |
+
<button type="submit" class="btn btn-primary">Check answers</button>
|
| 42 |
+
</form>
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
<div id="question-feedback" style="margin-top:16px">
|
| 46 |
+
</div>
|
| 47 |
+
|
| 48 |
</section>
|
|
|
|
| 49 |
|
| 50 |
+
{% if next_question %}
|
| 51 |
+
<div style="margin-top:12px; font-size:16px">
|
| 52 |
+
<a href="{% url 'quizzes:display_question' quiz.id next_question.id %}">→ Next question</a>
|
| 53 |
+
</div>
|
| 54 |
+
{% endif %}
|
| 55 |
+
</div>
|
| 56 |
+
|
| 57 |
+
<script>
|
| 58 |
+
const form = document.getElementById("question-form");
|
| 59 |
+
form.addEventListener("submit", (e) => {
|
| 60 |
+
e.preventDefault();
|
| 61 |
+
fetch(form.action, {method:'post', body: new FormData(form)})
|
| 62 |
+
.then((response) => response.text())
|
| 63 |
+
.then(text => {
|
| 64 |
+
document.getElementById("question-feedback").innerHTML = text;
|
| 65 |
+
});
|
| 66 |
+
});
|
| 67 |
+
</script>
|
| 68 |
</body>
|
| 69 |
</html>
|
quizzes/templates/quizzes/partial.html
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% if is_correct %}
|
| 2 |
+
✅ You got it!
|
| 3 |
+
{% else %}
|
| 4 |
+
❌ Sorry! The correct answer is {{ correct_answer }}
|
| 5 |
+
{% endif %}
|
quizzes/urls.py
CHANGED
|
@@ -6,7 +6,7 @@ app_name = 'quizzes'
|
|
| 6 |
|
| 7 |
urlpatterns = [
|
| 8 |
path('', views.index, name='index'),
|
| 9 |
-
path('<int:quiz_id>/', views.
|
| 10 |
-
path('<int:quiz_id>/
|
| 11 |
-
|
| 12 |
]
|
|
|
|
| 6 |
|
| 7 |
urlpatterns = [
|
| 8 |
path('', views.index, name='index'),
|
| 9 |
+
path('<int:quiz_id>/', views.display_quiz, name='display_quiz'),
|
| 10 |
+
path('<int:quiz_id>/questions/<int:question_id>', views.display_question, name='display_question'),
|
| 11 |
+
path('questions/<int:question_id>/grade/', views.grade_question, name='grade_question'),
|
| 12 |
]
|
quizzes/views.py
CHANGED
|
@@ -1,18 +1,32 @@
|
|
| 1 |
-
from django.shortcuts import get_object_or_404,render
|
|
|
|
| 2 |
|
| 3 |
-
from .models import Quiz
|
| 4 |
|
| 5 |
def index(request):
|
| 6 |
quiz_list = Quiz.objects.all()
|
| 7 |
context = {'quiz_list': quiz_list}
|
| 8 |
return render(request, 'quizzes/index.html', context)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
def display(request, quiz_id):
|
| 12 |
quiz = get_object_or_404(Quiz, pk=quiz_id)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
def
|
| 17 |
quiz = get_object_or_404(Quiz, pk=quiz_id)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from django.shortcuts import get_object_or_404,render, redirect
|
| 2 |
+
from django.urls import reverse
|
| 3 |
|
| 4 |
+
from .models import Quiz, Question
|
| 5 |
|
| 6 |
def index(request):
|
| 7 |
quiz_list = Quiz.objects.all()
|
| 8 |
context = {'quiz_list': quiz_list}
|
| 9 |
return render(request, 'quizzes/index.html', context)
|
| 10 |
|
| 11 |
+
def display_quiz(request, quiz_id):
|
|
|
|
| 12 |
quiz = get_object_or_404(Quiz, pk=quiz_id)
|
| 13 |
+
question = quiz.question_set.first()
|
| 14 |
+
return redirect(reverse('quizzes:display_question', kwargs={'quiz_id': quiz_id, 'question_id': question.pk}))
|
| 15 |
|
| 16 |
+
def display_question(request, quiz_id, question_id):
|
| 17 |
quiz = get_object_or_404(Quiz, pk=quiz_id)
|
| 18 |
+
# fetch ALL of the questions to find current and next question
|
| 19 |
+
questions = quiz.question_set.all()
|
| 20 |
+
current_question, next_question = None, None
|
| 21 |
+
for ind, question in enumerate(questions):
|
| 22 |
+
if question.pk == question_id:
|
| 23 |
+
current_question = question
|
| 24 |
+
if ind != len(questions) - 1:
|
| 25 |
+
next_question = questions[ind + 1]
|
| 26 |
+
return render(request, 'quizzes/display.html', {'quiz': quiz, 'question': current_question, 'next_question': next_question})
|
| 27 |
+
|
| 28 |
+
def grade_question(request, question_id):
|
| 29 |
+
question = get_object_or_404(Question, pk=question_id)
|
| 30 |
+
answer = getattr(question, 'multiplechoiceanswer', None) or getattr(question, 'freetextanswer')
|
| 31 |
+
is_correct = answer.is_correct(request.POST.get('answer'))
|
| 32 |
+
return render(request, 'quizzes/partial.html', {'is_correct': is_correct, 'correct_answer': answer.correct_answer})
|