1
所以我使用的表单模板设置动态的选择,我发现here动态形式返回KeyError异常
,因此我得到了
*forms.py*
class selectForm2(forms.Form):
def __init__(self, *args, **kwargs):
choices = kwargs.pop('my_choices')
super(selectForm2, self).__init__(*args, **kwargs)
self.fields["select_fields"] = forms.ChoiceField(choices=choices)
,并在我的意见,我有
form = selectForm2(my_choices = models())
哪里模型()创建元组,每个元组都有一个模型名称和一个模型表:
def models():
apps = get_app('Directories')
for model in get_models(apps):
model_classes.append((model._meta.verbose_name, model._meta.db_table),)
return model_classes
但是当我运行它,my_choices
通常在一个视图返回在choices = kwargs.pop('my_choices')
发布完整的回溯。它真的在'form = selectForm2(my_choices = models())'行抛出异常,还是在别的地方? – lanzz
我期望你有多种形式的实例,例如一次用于GET,一次用于POST,并且你忘记了在其中一个中传递“选择”。 –
确实......就是这样。我很困惑,因为调试器提到的KeyError是在forms.py不在我的意见... – per7inac1ousQ