以django形式处理动态表单提交 [英] Handling dynamic form submit in django forms
问题描述
我按照以下方式生成基于元数据的表单
I'm generating a form based on meta data in the following manner
class MeasureForm(forms.Form):
def __init__(self,measure_id,*args,**kwrds):
super(MeasureForm,self).__init__(*args,**kwrds)
m = Measure.objects.get(pk=measure_id);
if (m):
self.fields["measure_id"] = forms.IntegerField(initial = m.id , widget=forms.HiddenInput())
for mp in MeasureParameters.objects.filter(measure = m):
# get the NVL'ed copy of the parameter
p = mp.get_parameter_for_measure()
if not p.is_modifiable:
# the file has a constant value
if (p.values and p.default): # contant must have both values and default index
value_ = p.values[p.values.keys()[p.default-1]];
self.fields[p.name] = forms.IntegerField(label = p.description , initial = value_);
self.fields[p.name].widget.attrs['readonly'] = True;
else:
raise Exception("Parameter set as unmodifiable but has no value. [measure: %s, parameter: %s, measureparameter %s]"
% (measure_id , p.id , mp.__unicode__()))
elif (p.values):
# convert hstore dict to list of tuples for the choices to read
values_ = [(v, k) for k, v in p.values.iteritems()];
# set default if exists , else take the first item
default_ = values_[p.default-1][0] if p.default else values_[0][0]
self.fields[p.name] = forms.ChoiceField(label = p.description , choices = values_ , initial = default_)
else:
self.fields[p.name] = forms.IntegerField(label = p.description)
if (not p.is_visible):
self.fields[p.name].widget = forms.HiddenInput()
else:
raise Exception ("Could not find measure. [measure %s]" % (measure_id));
这是我的观点
def index(request,measure_id = None):
owners = Owner.objects.all()
form = None
result = None
title = None
# handle the form
if request.method == 'POST': # the form has been submitted
form = MeasureForm(request.POST)
if form.is_valid(): # All validation rules pass
result = 100;
else:
if (measure_id):
title = Measure.objects.get(pk=measure_id).name;
# make an unbound form
form = MeasureForm(measure_id)
return render(request, 'calc/index.html' ,
{'owners' : owners,
'form' : form ,
'title' : title ,
'result' : result})
我遵循此教程。
当我发回提交到索引视图时,我遇到问题。我收到以下错误
I followed this tutorial. I'm running into a problem when i issue submit back to the index view. i get the following error
int() argument must be a string or a number, not 'QueryDict'
我可以在转储中看到它在这一行失败了
I can see in the dump that it's fail in this line
m = Measure.objects.get(pk=measure_id);
所以我环顾四周,发现这篇文章。我试图将调用更改为这样的表单
So I looked around and found this post . I tried to change the call to the form like this
form = MeasureForm(request.POST, measure_id = request.POST.get('measure_id'))
我收到以下错误
__init__() got multiple values for keyword argument 'measure_id'
我的第一个问题是 - 为什么django再次执行init。已经创建了一个表单对象 - 字段只需要绑定到后台数据。
My first question is - Why is django executing the init again. a form object was already created - the fields just need to get bound form the post data.
其次,我该如何解决?
Second , how can I overcome this ?
** django newbie。谢谢。
**django newbie. Thanks.
推荐答案
从init方法中删除measure_id,而是使用 kwrds ['measure_id']
来检索它,然后适当地使用它们的关键字参数:
Remove measure_id from init method and instead use kwrds['measure_id']
to retrieve it inside, then use them appropriately for keyword argument:
MeasureForm(request.POST, measure_id=measure_id)
MeasureForm(measure_id=measure_id)
这篇关于以django形式处理动态表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!