delete()函数在django中不起作用,
def delete_hardware (request):
notice = ''
inventory = get_object_or_404(inventory, id = request.GET.get('id'))
if inventory:
inventory.delete()
notice = "Hardware was deleted"
return render_to_response('form.html', {'notice': notice},
context_instance=RequestContext(request))
......这些是我在form.html中的代码
<td bgcolor='#f1f1f1'><a onclick="javascript:confirmDelete('/delete/?id={{id}}')">
<img src='{{ MEDIA_URL }}/images/delete.png' width=25 height=25 />
</a></td>
答案 0 :(得分:1)
<强> views.py 强>
def delete_hardware (request, id):
inventory = get_object_or_404(inventory, pk=id).delete()
messages.success(request, "Hardware was deleted!")
return HttpResponseRedirect(reverse('app_name:url_name'))
<强> urls.py 强>
url(r'^delete/(?P<id>\d+)/$', 'delete_hardware',
name='delete_hardware'),
我无法提供完整的详细信息代码,因为您没有将完整的代码放在您的问题中。我不知道为什么,但这取决于你。这只是一个指南,我希望它能清除你的想法。如果表单提交了method = GET
,则只能获取数据<a href="/delete/{{value.id}}" class="delete-hardware">Delete</a>
<script>
$('.delete-hardware').click(function(){
var url = $(this).attr('href');
document.location.href = url;
});
</script>
答案 1 :(得分:0)
应为request.GET['id']
试试
def delete_hardware (request):
notice = " "
id = request.GET['id']
try:
inv = inventory.objects.get(id = id)
inv.delete()
notice = "Hardware was deleted"
except inventory.DoesNotExist:
raise Http404
return render_to_response('form.html', {'notice': notice},
context_instance=RequestContext(request))