I'm following this tutorial with a few changes: http://www.jacobian.org/writing/dynamic-form-generation/, all of which I'll post as snippets below. When I attempt to access a page that goes through my dynamic form view it throws a TypeError. Here's the traceback: http://dpaste.com/793196/.
forms.py (testing/forms.py)
from django import forms
class TestForm(forms.Form):
test_id = forms.CharField()
user_id = forms.CharField()
complete_time = forms.IntegerField()
def __init__(self, *args, **kwargs):
extra = kwargs.pop('extra')
super(TestForm, self).__init__(*args **kwargs)
for i, question in enumerate(extra):
self.fields['custom_%s' % i] = forms.CharField(label=question)
def extra_answers(self):
for name, value in self.cleaned_data.items():
if name.startswith('custom_'):
yield (self.fields[name].label, value)
views.py (testing/views.py)
def exam(request, test_id):
user = request.user
t = get_object_or_404(Test, pk=test_id)
if user.is_authenticated():
extra_questions = get_questions(request, test_id)
if request.method == 'POST':
form = TestForm(request.POST or None, extra=extra_questions)
if form.is_valid():
for (question, answer) in form.extra_answers():
save_answer(request, question, answer)
return HttpResponseRedirect('/tests/')
else:
form = TestForm(None, extra=extra_questions)
return render(request, 'testing/exam.html', { 't' : t, 'form' : form })
else:
return HttpResponseRedirect('/')
def get_questions(request, test_id):
t = get_object_or_404(Test, pk=test_id)
questions = t.question_set.all()
for question in questions:
title = question.question
qlist = []
qlist.append(title)
Any help would be appreciated as I'm racking my mind for an answer.
You accidentally forgot the comma.
super(TestForm, self).__init__(*args, **kwargs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With