1
我正在跟隨本教程進行一些更改:http://www.jacobian.org/writing/dynamic-form-generation/,我將在下面以片段形式發佈所有這些內容。當我嘗試訪問通過我的動態表單視圖的頁面時,它會引發TypeError。這是回溯:http://dpaste.com/793196/。不受支持的操作數類型**或pow():'tuple'和'dict'
forms.py(測試/ 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(測試/ 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)
爲I」任何幫助,將理解的爲了答案而絞盡腦汁。
總是顯示你的錯誤。 – Marcin
我粘貼了錯誤的全部回溯。還有什麼顯示? –
您不鼓勵使用外部粘貼網站,至少單獨使用。如果有瘋狂的數量,請使用dpaste,但始終將最重要的部分放在您的問題中。 – Marcin