我的上下文处理器中有item变量来计算购物车项目并在页面上显示。如何用ajax做到这一点?并在用户添加新项目时更新购物车项目数量?
context_processors.py
def ToyCartItems(request):
try:
user_cart = Cart.objects.get(user=request.user)
cart_items = user_cart.cart_item.all()
except Cart.DoesNotExist:
cart_items = 0
return {'NumberOfItems': cart_items}