Multiple Choice Quiz

1,171 views
Skip to first unread message

jbr3

unread,
Mar 10, 2012, 6:16:04 PM3/10/12
to Django users
Hi,

I'm trying to build a quiz app. The quizzes will have varying numbers
of questions and a radio button next to each of 4 potential answers
(there will only be one correct answer for each question). I want to
save the user's answers in the database and automatically grade the
quiz by comparing it to the actual answers. I have a model for the
question and a model to add answers so far, set up kind of like the
poll tutorial. That way I can add both questions and answers from the
admin (and reuse both). What I can't understand is how I can save the
radio button responses and generate the actual form for the quiz-
taker. I know, if I'm saving the user's quiz, it has to be saved in
the model. It seems like I should be using a ModelForm for that. But I
also want to generate the questions and potential answers in the same
template with the radio buttons. Should I be populating the form with
the question and answer objects from the two models and then
generating radio buttons (related to a third model) in the template ?
Sorry, I know this isn't so clear. Am I just way off in how I'm doing/
thinking of this problem ? Could you give a general explanation of how
this should be done or point me to an app that already does this
(which I can use/examine code from) ?

Thanks in advance

John S. Dey

unread,
Mar 10, 2012, 8:20:23 PM3/10/12
to django...@googlegroups.com
You might try reading the tutorial. It provides example of a form.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

jbr3

unread,
Mar 11, 2012, 2:13:03 PM3/11/12
to Django users
I've gone through the tutorial. But I would just like to get a general
idea of the best way to incorporate all these elements so I can save
and reuse them.

Shawn Milochik

unread,
Mar 11, 2012, 2:18:26 PM3/11/12
to django...@googlegroups.com
On 03/11/2012 02:13 PM, jbr3 wrote:
I've gone through the tutorial. But I would just like to get a general
idea of the best way to incorporate all these elements so I can save
and reuse them.



What have you tried, and what specific issues have you run into?

If you ask a question and nobody seems helpful, please read this and try a different approach:

https://code.djangoproject.com/wiki/UsingTheMailingList

Shawn

jbr3

unread,
Mar 11, 2012, 3:50:53 PM3/11/12
to Django users
Really, the problem is that I can't think of the best way to do it. I
know I want to reuse the questions and the answers that go along with
them. And I would like to be able to add new ones easily. So I tried
to use two different models for this: one for the questions and one
for the possible answer choices that go with each. So it basically
looks like the poll app set up. It seems like storing the radio button
answers would necessitate a third model. I would also need a fourth
model for the actual answers, correct ? I can't really see how all of
it should be combined. I would be happy just to know the best way to
combine the radio buttons with the potential answers. Am I correct
that a third model is needed to store the value for the button the
user selects ? Would this be a ModelForm that works with a single-
field model storing the value selected from the button ?

Shawn Milochik

unread,
Mar 11, 2012, 4:11:25 PM3/11/12
to django...@googlegroups.com
I think I'd do this:

Models:

Question
question text

Answer
question foreign key
answer text
correct (boolean)

Guess
user foreign key
answer foreign key

That should be all you need (along with the User model or your own
method of tracking unique users without forcing them to register).

I don't understand what your fourth model is for.


jbr3

unread,
Mar 11, 2012, 5:16:31 PM3/11/12
to Django users
Thanks for responding Shawn. I guess I was thinking that if I wanted
to save the user's responses and the correct answers a different model
would be needed for both.

jbr3

unread,
Mar 14, 2012, 11:36:00 PM3/14/12
to Django users
Can anyone help me with some questions I still have about this ? I
haven't had much experience with web programming, so trying to
understand this is kind of challenging.

1. I created the models Shawn suggested. And I'm now able to add a
question and possible answers to it in the admin area. Each possible
answer has a checkbox next to it with the "Correct" label from the
model. Does setting the correct response require anything more than
selecting the appropriate checkbox and saving the question ?

2. I still don't understand how I'm supposed to combine these three
models together in order to get the right output for the user. I've
been trying different approaches in views.py, forms.py and my
template. But I can't really figure out how this should be set up. I
don't like to keep asking for such direct examples.
But would anyone be able to provide kind of an abstract view based on
the model Shawn provided.

jbr3

unread,
Mar 23, 2012, 6:19:41 PM3/23/12
to Django users
Hi again,

I've been trying to figure this out for awhile, but to no avail. I'll
try to list the problems I've had in understanding it.

1. I'm not sure what the forms.py file should look like.

Going by Shawn's model, would it be something like:

" class GuessForm(ModelForm):
class Meta:
model= Guess
exclude = ('user') "

This gives me a dropdown list of possible answers, but no form changes
I've tried to make give me multiple radio buttons or multiple
checkboxes. Have I selected the wrong model to make the modelform
from ?

2. I don't completely understand the concept of using Booleans. I've
added admin functionality to the app so I can add questions and
potential answers as well as select (using a checkbox) the correct
response. Based on the model Shawn provided, selecting one of the
checkboxes generated in admin makes that choice true and the others
false. The user's answer needs to be compared against these, but I
don't understand how I would achieve that. I assume I'm supposed to
set this up in the form, but I can't get the radio buttons or
checkboxes to work there. I can successfully submit an answer from the
dropdown, but I still have no idea how it would be compared to the
stored boolean value. Do I have to write more code for this ? And what
would the best way be to retrieve it ?

Also, in the examples I've seen, BooleanField() in forms involves
creating a list of choices in the form. But it doesn't seem like I
would need such a thing if I'm just using radio buttons whose value
will be checked against a changing option for each question. Yet I
can't really see how an alternative approach would work.

3. I don't know what the template should look like. It seems like I
should be mixing the form fields with data passed directly from the
model by using views. In my view, I've set
"Question.objects.get(id=1)" to a variable. I've passed that to the
template. But I've also used "{{ form.as_p }}". That gives me the
possible answers that had been entered in the admin in the template.
But, the answers aren't generated as radio buttons with text next to
them. It seems like I would have to pass the actual text of the
answers using views (setting "variable.answer_set.all() to another
variable and then passing it to the template) and then generate radio
buttons some other way. When I generated the buttons in the template,
though, the submission failed. I don't see how these could be checked
against the boolean value stored for the right answer. And, like I
said, I can't figure out how to make them work properly with the
form.

I know I've asked this already, but is it possible for anyone to show
some sample code so I can understand how this should be done using
Shawn's models. I've been trying to get this for awhile now, but I
still don't really understand it. I know there are about fifty
different questions here, but if anyone could help out in any way it
would be appreciated.

Thanks

Babatunde Akinyanmi

unread,
Mar 24, 2012, 3:08:52 AM3/24/12
to django...@googlegroups.com
Hi jbr3,
Check scrabala.com
Is it similar to what you are working on?

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

jbr3

unread,
Mar 24, 2012, 11:46:47 AM3/24/12
to Django users
Babatunde,

Yes, that's basically the same idea. The questions would all be on the
same page, however, and I wouldn't incorporate the time limit. But,
that's mostly what I was thinking of.

On Mar 24, 3:08 am, Babatunde Akinyanmi <tundeba...@gmail.com> wrote:
> Hi jbr3,
> Check scrabala.com
> Is it similar to what you are working on?
>

Babatunde Akinyanmi

unread,
Mar 26, 2012, 6:42:32 AM3/26/12
to django...@googlegroups.com
Ok. Let me send you a gist about how I achieved mine to give you an idea.

Babatunde Akinyanmi

unread,
Mar 26, 2012, 7:00:21 AM3/26/12
to django...@googlegroups.com
jbr3,
https://gist.github.com/2204408
The code is most likely buggy and might be insecure because I'm not really sure if hackers can use radio buttons to submit malicious stuff.
The idea is using the model structure Shawn gave. I created a custom form class which extracts some Question instances. Since Answers foreignKey to Questions, the answers linked to each question can be retrieved. There is a function that takes these Questions and its related Answers and then outputs the html for the form such that the Answers are displayed as radio buttons. If the custom form comes in with data from the request, the customForm determines how many of them are correct. If you go through the code you would see how the boolean in Answer can be used to determine the correct answer.
We can talk about my fee later :D ;)
If you still have issues let me know.

jbr3

unread,
Mar 26, 2012, 3:56:40 PM3/26/12
to Django users
Thank you for taking the time to help Babatunde; it's very much
appreciated.


On Mar 26, 7:00 am, Babatunde Akinyanmi <tundeba...@gmail.com> wrote:
> jbr3,https://gist.github.com/2204408
> The code is most likely buggy and might be insecure because I'm not really
> sure if hackers can use radio buttons to submit malicious stuff.
> The idea is using the model structure Shawn gave. I created a custom form
> class which extracts some Question instances. Since Answers foreignKey to
> Questions, the answers linked to each question can be retrieved. There is a
> function that takes these Questions and its related Answers and then
> outputs the html for the form such that the Answers are displayed as radio
> buttons. If the custom form comes in with data from the request, the
> customForm determines how many of them are correct. If you go through the
> code you would see how the boolean in Answer can be used to determine the
> correct answer.
> We can talk about my fee later :D ;)
> If you still have issues let me know.
>
> On Mon, Mar 26, 2012 at 11:42 AM, Babatunde Akinyanmi
> <tundeba...@gmail.com>wrote:
>
> > Ok. Let me send you a gist about how I achieved mine to give you an idea.
>
Reply all
Reply to author
Forward
0 new messages