我希望能够通过现有记录的下拉列表和所选记录的删除操作向用户显示表单。到目前为止,我所能找到的是将记录的id传递给表单的示例(如下所示),如果我在函数中对id进行硬编码,我可以使用它,但这显然不是解决方案。我错过了什么?感谢。
def DeleteRecord(request, id):
to_delete = get_object_or_404(MyModel, id=id)
#+some code to check if this object belongs to the logged in user
if request.method == 'POST':
form = DeleteRecordForm(request.POST, instance=to_delete)
if form.is_valid(): # checks CSRF
to_delete.delete()
return HttpResponseRedirect("/")
else:
form = DeleteRecordForm(instance=to_delete)
data = {'form': form}
return render(request, 'deleteprimerpair.html', data)