Django forms for quiz app ( how to disable a form after submission )


I am creating a classroom app. Which has a exam section where students can give test. If a student wants to give a exam he/she has to select subject and then has to go in exam section. I want student to have access to exam form for one time.


One possibility to do this is as follows. Note that there are many more options available to you, which one is the easiest depends on your entire application.

Keep track of your students’ exam results:

class ExamResult(Model):
    student = ForeignKey(User, related_name='exam_results')
    exam = ForeignKey(Exam, related_name='exam_results')

In your form template, check whether the student has already an ExamResult object associated with the exam. You could do this in two parts:

In your context builder, define a variable with all the new exams.

exams_taken = ExamResult.objects.filter(student=user).values_list('exam_id', flat=True)
new_exams = Exam.objects.exclude(id__in=exams_taken)

In your form, loop over the new_exams only.

{% for exam in new_exams %}
    {# show the form for this exam #}
{% endfor %}

Answered By – physicalattraction

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published