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