**或pow()不支持的操作数类型:'tuple'和'dict'

Ros*_*ker 2 django

我正在按照本教程进行一些更改:http://www.jacobian.org/writing/dynamic-form-generation/,所有这些我将在下面作为片段发布.当我尝试访问通过动态表单视图的页面时,它会抛出TypeError.这是追溯: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)
Run Code Online (Sandbox Code Playgroud)

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) 
Run Code Online (Sandbox Code Playgroud)

任何帮助都会受到赞赏,因为我正在努力寻找答案.

Ign*_*ams 16

你不小心忘记了逗号.

super(TestForm, self).__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

  • 太疯狂了,我花了 30 分钟查看代码,就像“我知道某处缺少一个逗号”,但我找不到它。谷歌搜索,阅读这个答案,回顾代码,不到 10 秒就发现缺少逗号。 (2认同)