Monday, April 1, 2013

How to create a form list for a Model Form dynamically?

I am trying to make a simple quiz app.

I am trying to go to the URL /quiz/<quiz_id> and using the quiz_id which has a list of questions associated with the quiz id and create a list of forms to use in the model form.

In the urls.py file I know you can pass a list of forms to be used, but I need to create the list of forms at runtime.

I have tried passing the return value of a function as parameter but can't figure out the syntax.

(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 I create a view and I create the form list inside the view and then call QuizWizard.as_view

#views.py  class QuizWizard(SessionWizardView):      def __init__(self, **kwargs):          self.form_list = kwargs.pop('form_list')          return super(QuizWizard, self).__init__(**kwargs)        def done(self, **kwargs):          return render_to_response('done.html', {              'form_data':[form.cleaned_data for form in self.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)            #quiz_wizard = QuizWizard()      return QuizWizard.as_view(form_list=question_forms)(request)

But I am getting the error 
issubclass() arg 1 must be a class


My issue is syntax either way I can't figure out how to call the QuizWizard.as_view(). Here are related files:

#forms.py
class QuestionForm(forms.ModelForm):      class Meta:          model = Question

#urls.py
urlpatterns = patterns ('',      url(r'^(?P<quiz_id>\d+)', 'quiz.views.get_form_list'),  )

#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

Full Traceback: http://bpaste.net/show/0YNrLYJSdjdJ7Hea5q1c/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment