我有一个动态表单,并且由于参数“waysNumber”而导致 post 方法出错。问题出在 post 方法中的“formulari = WayForm(request.POST, request.FILES)”行中。它从 WayForm 执行函数 init(),现在没有名为waysNumber 的参数。传递此参数的正确方法是什么?这是我的代码:
表格.py:
class WayForm(forms.Form):
def __init__(self, *args, **kwargs):
waysNumber = kwargs.pop("waysNumber")
super(WayForm, self).__init__(*args, **kwargs)
CHOICES = (('1','Pista',),('2','Corriol',))
for i in range(waysNumber):
self.fields["Descripcio"+str(i)]= forms.CharField(widget=forms.Textarea(),label="Descripcio"+str(i))
self.fields["Tipus"+str(i)] = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES, label="Tipus de camí"+str(i))
视图.py:
def acoplar_track(request, track_id):
if request.method=='GET':
track = get_object_or_404(Track, id=track_id) # obte el track i la zona corresponent al track
newWays_l = x.getTrackWays() # obte les ids dels nous camins que s'afegiran
formulari = WayForm(waysNumber = len(newWays_l))
return render(request,'principal/wayForm.html')
else:
formulari = WayForm(request.POST, request.FILES)
if formulari.is_valid():
...
return render_to_response('principal/inici.html', context_instance=RequestContext(request))
谢谢你的帮助!