据我了解,您有一个需要进行测验的字段列表。所以列表看起来像这样:
questions = (
('Place your 1 answers %s and %s.', ('field_1_1', 'field_2_2')),
('Place your 2 answers %s and %s.', ('field_2_1', 'field_2_2')),
)
您可以像这样为每个字段创建表单:
class QuizzForm(forms.Form):
def __init__(self, *args, **kwargs):
super(QuizzForm, self).__init__(*args, **kwargs)
for question in questions:
for field in question[1]:
self.fields[field] = forms.ChoiceField()
并在与此类似的帖子后访问字段:
if form.is_valid():
for question in questions:
for field in question[1]:
answer = form.cleanded_data.get(field)