我有以下在Django模板中手动呈现的表单。通过“手动渲染”,我的意思是代码中没有相应的Form或ModelForm对象(不使用Form对象的原因/细节有点复杂,并且可以安全地排除在这个问题之外)。我现在有一个心理障碍,我似乎无法弄清楚如何在Django视图中以可用的形式获取这些数据。具体来说,当表单被POST时,我如何获得各个和attendance_remarks
的每个workers_pks
?在Django中从手动呈现的表单获取合理的数据
<form>
<table>
<thead>
<tr>
<th>Sl</th>
<th>Worker</th>
<th>Worker's Gender</th>
<th>Work</th>
<th>Unit cost of work</th>
<th>Job</th>
<th>Attendance</th> {# 1 or less if unit is day, 8 or less if unit is hours #}
<th>Remarks</th> {# if any #}
</tr>
</thead>
<tbody>
{% for result in results %}
<tr>
<th>{{ forloop.counter }}</th>
<td>{{ result.worker.name }} <input type="hidden" name="workers_pks" value="{{ result.worker.pk }}" id="id_workers_pks_{{ forloop.counter }}"></td>
<td>{{ result.worker.get_gender_display }}</td>
<td>{{ result.work.name }}</td>
<td>{{ result.work.unit_cost }} per {{ result.work.work_unit }}</td>
<td>{{ result.job_handover.job.name }}</td>
<td>
<input type='text' name='attendance_value' id='id_attendance_value_{{ forloop.counter }}' />
</td>
<td>
<input type='text' name='attendance_remarks' id='id_attendance_remarks_{{ forloop.counter }}' />
</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
只是为了澄清,我知道我可以做的事情得到的值一样
workers = request.POST.getlist('workers_pks')
attendance_values = request.POST.getlist('attendance_values')
attendance_remarks = request.POST.getlist('attendance_remarks')
我最关心的是如何得到的适当attendance_value
和attendance_remarks
每个对应workers_pks
为什么不使用FormSet? https://docs.djangoproject.com/en/dev/topics/forms/formsets/ – 2013-02-17 09:32:36