0

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>
4

2 回答 2

2

视图.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'))

网址.py

url(r'^delete/(?P<id>\d+)/$', 'delete_hardware', 
    name='delete_hardware'),

我无法提供完整的详细代码,因为您没有将完整的代码放在您的问题中。我不知道为什么,但这取决于你。这只是一个指南,我希望它能清除您的想法。如果您的表单已提交方法=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>
于 2013-02-06T07:51:10.967 回答
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))
于 2013-02-06T07:16:46.657 回答