如何使用ajax django重定向

时间:2016-05-07 07:27:15

标签: ajax django

我在这里面临的问题是,当项目数为0时,我无法看到cartempty页面。 这是视图

if request.is_ajax():
    if request.POST.get('action') == 'remove':
    cart_item.delete()

    if cart_item.cart.items.count() == 0:
        return render(request , "carts/cartempty.html", {})
    else:
        try:
            total = cart_item.line_item_total
        except:
            total = None
        try:
            original_total = cart_item.cart.original_total
        except:
            original_total = None

        try:
            final_total = cart_item.cart.final_total
        except:
            final_total = None

        try:
            total_items = cart_item.cart.items.count()
        except:
            total_items = 0


        data = {
            "line_total": total,
            "original_total": original_total,
            "final_total" :final_total,
            "total_items": total_items
        }
        return JsonResponse(data)

如何使用ajax重定向到cartempty页面?

1 个答案:

答案 0 :(得分:0)

猜猜。你试过吗?

return render_to_response('carts/cartempty.html', context_instance=RequestContext(request))

OR

return HttpResponseRedirect("/carts/cartempty/")

而不是:

return render(request , "carts/cartempty.html", {})