我最初使用kwargs
填充我的djnago表单,
Forms.py
class myForm(Forms.Form):
textbox1 = forms.CharField()
dropdown1 = forms.ChoiceField()
def __init__(self,*args,**kwargs):
choices = kwargs.pop('choices')
super(myForm,self).__init__(*args,**kwargs)
self.fields['dropdown1'] = forms.ChoiceField(choices=choices)
views.py
def myjob(req):
ch = {("1","A"),("2","B")}
if req.method == "POST":
frm = myForm(data=req.POST) # throws error saying can't find choices in Forms.py
else:
frm = myForm(choices=ch) # no problem here
此代码在提交数据时发出错误,因为它试图再次调用 init 。 我该如何正确提交?
答案 0 :(得分:1)
错误似乎很清楚:代码期待choices
kwarg,而你没有在POST上提供它。你可以简单地添加它:
if req.method == "POST":
frm = myForm(data=req.POST, choices=ch)