我正在使用jQuery.formsets在Django模板下动态创建更多表单。问题是我得到输入到数据库的重复值。
据我所知,jQuery.formsets生成的ID是正确的,因为当我在视图下打印出request.POST时,它会显示正确的值。
在点击提交之前,有问题的值是“Observer ONE / TWO / THREE”,其中包含以下ID:
request.POST:
<QueryDict: {u'form-1-form-INITIAL_FORMS': [u''], u'form-1-form-MAX_NUM_FORMS': [u''], u'commune': [u'Tartu'], u'additional_workgroups_view_0': [u''], u'form-0-observed_by_1': [u'3'], u'form-0-observed_by_0': [u'Observer ONE'], u'sample': [u''], u'remarks': [u''], u'access_edit': [u'private'], u'district': [u'Tartu'], u'country_1': [u'EE'], u'access_view': [u'public'], u'form-2-form-MAX_NUM_FORMS': [u''], u'substrate_1': [u''], u'substrate_0': [u''], u'geom': [u''], u'exact_date_and_time': [u''], u'latitude': [u'25.36698180'], u'csrfmiddlewaretoken': [u'AJTXFtEYQbX0pdBdvSe53443HKHN5dvg'], u'method': [u'MAP'], u'coordinate_uncertainty_in_meters': [u'200'], u'form-1-form-TOTAL_FORMS': [u''], u'elevation': [u''], u'country_0': [u'Estonia'], u'timespan_begin': [u'25-03-2013 11:08'], u'locality_text': [u'Ravila 19'], u'additional_users_view_0': [u''], u'timespan_end': [u''], u'form-TOTAL_FORMS': [u'3'], u'form-INITIAL_FORMS': [u'0'], u'form-2-form-INITIAL_FORMS': [u''], u'taxon_node_0': [u'Fungi'], u'taxon_node_1': [u'5'], u'district_ehak': [u''], u'form-2-observed_by_1': [u'3'], u'form-2-observed_by_0': [u'Observer THREE'], u'form-1-observed_by_0': [u'Observer TWO'], u'form-1-observed_by_1': [u'1'], u'form-2-form-TOTAL_FORMS': [u''], u'commune_ehak': [u''], u'additional_users_edit_0': [u''], u'form-MAX_NUM_FORMS': [u''], u'study': [u'2'], u'longitude': [u'15.69302410'], u'additional_workgroups_edit_0': [u''], u'remarks_sample': [u''], u'kingdom_helper': [u'5']}>
但是,当我打印出formset本身时,form0和form2值似乎是重复的。
<input type="hidden" name="form-TOTAL_FORMS" value="3" id="id_form-TOTAL_FORMS" />
<input type="hidden" name="form-INITIAL_FORMS" value="0" id="id_form-INITIAL_FORMS" />
<input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS"/>
<tr>
<th>
<label for="id_form-0-observed_by_0"></label>
</th>
<td>
<input style="width: 200px" name="form-0-observed_by_0" required="True" value="Observer THREE" id="id_form-0-observed_by_0" data-selectable-type="text" data-selectable-allow-new="false" data-selectable-url="/selectable/agent-personlookup/" type="text" class="span12"/>
<input data-selectable-type="hidden" name="form-0-observed_by_1" id="id_form-0-observed_by_1" required="True" type="hidden" style="width: 200px" value="3" class="span12" />
</td>
</tr>
<tr>
<th>
<label for="id_form-1-observed_by_0"></label>
</th>
<td>
<input style="width: 200px" name="form-1-observed_by_0" required="True" value="Observer ONE" id="id_form-1-observed_by_0" data-selectable-type="text" data-selectable-allow-new="false" data-selectable-url="/selectable/agent-personlookup/" type="text" class="span12"/>
<input data-selectable-type="hidden" name="form-1-observed_by_1" id="id_form-1-observed_by_1" required="True" type="hidden" style="width: 200px" value="1" class="span12"/>
</td>
</tr>
<tr>
<th>
<label for="id_form-2-observed_by_0"></label>
</th>
<td>
<input style="width: 200px" name="form-2-observed_by_0" required="True" value="Observer THREE" id="id_form-2-observed_by_0" data-selectable-type="text" data-selectable-allow-new="false" data-selectable-url="/selectable/agent-personlookup/" type="text" class="span12"/>
<input data-selectable-type="hidden" name="form-2-observed_by_1" id="id_form-2-observed_by_1" required="True" type="hidden" style="width: 200px" value="3" class="span12"/>
</td>
</tr>
查看:
def observation_add(request):
if request.method == 'POST':
print 'REQUEST POST START'
print request.POST
print 'REQUEST POST END'
ObservedByFormset = formset_factory(ObservedByForm)
observed_by_formset = ObservedByFormset(request.POST)
print 'DEBUG: FORM BEFORE VALID:', observed_by_formset
答案 0 :(得分:1)
原来是jquery.dj.selectable.js正在改变隐藏的字段值,因此弄乱了后端。该脚本必须完全放在表格div和for循环之间。
{% for form in formset.forms %}
<script type="text/javascript" src="/static/selectable/js/jquery.dj.selectable.js"></script>
<div id="table_rel">