def add_to_cart(request, pk): current_user = request.user product = Products.objects.get(pk=pk) # add product to cart c = Cart(user=current_user, product_name=product, quantity=1) # save to disk c.save() # get number of items in cart for current user cart_dict = Cart.objects.filter(user=current_user).aggregate( Sum('quantity')) cart_list = [int(value) for value in cart_dict.values()] products = Products.objects.all() return render(request, "app/products.html", { "cart": cart_list[0], "products": products })
def register(request): if request.method == 'POST': body_unicode = request.body.decode('utf-8') body = json.loads(body_unicode) if body['password'] != body['password1']: return JsonResponse({'status': False, 'message': 'error passwords'}) try: User.objects.get(username=body['username']) return JsonResponse({'status': False}) except User.DoesNotExist: user = User(username=body['username'], password=body['password'], email=body['email']) user.save() cart = Cart(user=user) cart.save() return JsonResponse({'status': True})