我有一個用戶列出了他在用戶配置文件中輸入的學校列表。我想讓他有能力刪除他的任何一個條目。獲取請求中密鑰值的最簡單方法.POST
以下是我目前使用的刪除數據庫錄入的方式,基於一鍵在模板中的值:
# in template
{% for education in educations %}
<p>{{ education.school }}
<input type="submit" name="delete_{{education.id}}" value="Delete" /></p>
{% endfor %}
# in view
if 'Delete' in request.POST.values():
for key in request.POST.keys():
if key.startswith('delete'):
education_id = key[7:]
profile.educations.remove(Education.objects.get(id=education_id))
是否有更簡單的方式來獲得一個鍵的值,而不是必須遍歷for key in request.POST.keys()
?謝謝。