【发布时间】:2011-08-30 17:21:06
【问题描述】:
使用 django,我有一个表单,用户可以在其中输入他的'position'。用户可以添加多个位置。用户也可以删除职位。有两点值得注意:
1) 在表单中,有两个按钮,'Add' 和'Delete'。
2) 我在模板中使用 for 循环来填充位置列表和删除按钮。
这是我目前拥有的:
# in template
<tr>
<td>Position</td>
<td>{{ form.position }}
<input type="submit" value="Add" , name='action'/>
</td>
</tr>
<tr>
<td> </td>
<td>
{% for position in positions %}
{{ position}}
<input type="submit" value="Delete {{position }}", name='action'/>
{% endfor %}
</td>
</tr>
# in views.py
...
if action == 'Add':
positions.append(request.POST['position'])
return render_to_response(...)
if 'Delete' in action:
positions.remove(request.POST['action'][7:])
return render_to_response('...)
这似乎是一种非常不雅的方式来执行“删除”部分。
有没有更好的方法来获取position 的值,而不必在'Delete' submit button 中塞入额外的信息,然后将其切片以获得它的值?
【问题讨论】:
标签: django forms django-models django-templates