I am trying to use Form Wizard but I can't figure out where to set the choices for the fields.
#views.py
class QuizWizard(SessionWizardView):
def done(self, form_list, **kwargs):
return render_to_response('done.html', {
'form_data':[form.cleaned_data for form in form_list],
})
#forms.py
class QuestionForm(forms.ModelForm):
#selection = forms.ChoiceField()
class Meta:
model = Question
I see an empty form that looks like the admin panel for adding an object. I would like to be able to pass a question to the form and have the question field filled out and not editable and preferable not submitted.
If I do
(r'^(?P<quiz_id>\d+)', QuizWizard.as_view(get_form_list)),
the function get_form_list has no length
(r'^(?P<quiz_id>\d+)', QuizWizard.as_view(get_form_list(quiz_id))),
Quiz_id is unknown.
so now I am trying to pass quiz_id to the view function and generate the list of question forms to be used in the form wizard
url(r'^(?P<quiz_id>\d+)', 'quiz.views.get_form_list'),
class QuizWizard(SessionWizardView):
def done(self, form_list, **kwargs):
return render_to_response('done.html', {
'form_data':[form.cleaned_data for form in form_list],
})
def get_form_list(request, quiz_id):
quiz = Quiz.objects.get(id=quiz_id)
question_forms = []
for question in quiz.questions.all():
choices = []
for choice in question.choices.all():
choices.append(choice)
f = QuestionForm(instance=question)
question_forms.append(f)
return QuizWizard.as_view(question_forms)(request)
I am getting the error message
issubclass() arg 1 must be a class
Update based on Rohan's answer:
def get_form_list(request, quiz_id):
quiz = Quiz.objects.get(id=quiz_id)
question_forms = []
for question in quiz.questions.all():
choices = []
for choice in question.choices.all():
choices.append(choice)
f = QuestionForm(instance=question)
question_forms.append(f)
inst_dict = {}
for idx, question in enumerate(question_forms):
inst_dict[str(idx)] = question
print inst_dict
#inst_dict = { str(index(x)) : x for x in question_forms}
QuestFormList = []
for i in range(len(question_forms)):
QuestFormList.append(QuestionForm)
QuizWizard.as_view(QuestFormList, instance_dict=inst_dict)(request)
With this code I am getting an error
'ModelFormOptions' object has no attribute 'many_to_many'
Here is my models.py
class Choice(models.Model):
choice = models.CharField(max_length=64)
def __unicode__(self):
return self.choice
#create a multiple choice quiz to start
class Question(models.Model):
question = models.CharField(max_length=64)
answer = models.CharField(max_length=64)
choices = models.ManyToManyField(Choice)
module = models.CharField(max_length=64)
def __unicode__(self):
return self.question
class Quiz(models.Model):
name = models.CharField(max_length=64)
questions = models.ManyToManyField(Question)
def __unicode__(self):
return self.name
You should call it using the class instead of object. So change your call to
QuizWizard.as_view(question_forms)(request)
Update:
The wizard view takes form class list as parameters not the form instance. You are creating form instances in question_forms
and passing it to view.
If you want to pass instance for the form in each step, you can pass instance_dict
.
Something like ...
inst_dict = { '0': question_forms[0], #step 0 instance
'1': question_forms[1], #step 1 instance
}
QuestFormList = [QuestionForm, QuestionForm ...]
QuizWizard.as_view([QuestFormList, instance_dict=inst_dict)(request)