gpt4 book ai didi

django - **或pow()不受支持的操作数类型: 'tuple' and 'dict'

转载 作者:行者123 更新时间:2023-12-01 07:02:42 27 4
gpt4 key购买 nike

我在本教程中进行了一些更改:http://www.jacobian.org/writing/dynamic-form-generation/,所有这些我都将在下面作为摘要发布。当我尝试访问经过动态表单 View 的页面时,它将引发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)

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)

在我竭尽全力寻求答案的过程中,我们将不胜感激。

最佳答案

您不小心忘记了逗号。

super(TestForm, self).__init__(*args, **kwargs)

关于django - **或pow()不受支持的操作数类型: 'tuple' and 'dict' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12182467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com