Reputation: 459
I am creating a MCQ web application in which user will select an answer and application will return the answer is correct or not on the spot.
models.py:
from django.db import models
#Tuple
SUBJECTS = (
('python','Python'),
('sql', 'SQL'),
('javascript','JavaScript'),
('java','Java'),
)
class Question(models.Model):
subject = models.CharField(max_length=100, choices=SUBJECTS, default=None)
question = models.TextField()
opt1 = models.TextField()
opt2 = models.TextField()
opt3 = models.TextField(null=True)
opt4 = models.TextField(null=True)
correct_answer = models.TextField()
def __str__(self):
return self.question
views.py:
def practice(request):
questions = Question.objects.all()
return render(request,"practice.html",{'questions':questions})
practice.html:
{% for i in questions %}
<div class="container border"><br />
<h2>{{ i.question }} : </h2>
<b>
<hr></b>
<div class="form-check p-2 ">
<input class="form-check-input" type="radio" name="{{ i.id }}" id="{{ i.opt1 }}" value="{{ i.opt1 }}">
<label class="form-check-label" for="{{ i.opt1 }}">
{{ i.opt1 }}
</label>
</div>
<hr>
<div class="form-check p-2 ">
<input class="form-check-input" type="radio" name="{{ i.id }}" id="{{ i.opt2 }}" value="{{ i.opt2 }}">
<label class="form-check-label" for="{{ i.opt2 }}">
{{ i.opt2 }}
</label>
</div>
<hr>
<div class="form-check p-2 ">
<input class="form-check-input" type="radio" name="{{ i.id }}" id="{{ i.opt3 }}" value="{{ i.opt3 }}" >
<label class="form-check-label" for="{{ i.opt3 }}">
{{ i.opt3 }}
</label>
</div>
<hr>
<div class="form-check p-2 ">
<input class="form-check-input" type="radio" name="{{ i.id }}" id="{{ i.opt4 }}" value="{{ i.opt4 }}">
<label class="form-check-label" for="{{ i.opt4 }}">
{{ i.opt4 }}
</label>
</div>
</div>
<label hidden>{{ i.correct_answer }}</label>
<br />
{% endfor %}
Output:
Now I want to show user that he has selected correct answer or incorrect, correct answer is saved in {{ i.correct_answer }}
.
Can anyone help me how to achive this?
Upvotes: 0
Views: 1113
Reputation: 239
you should add a submit button in your html, that will post the answers to your db. Then, call the object model in views.py, instantiate it, and query it the way you want.
Upvotes: 1