Dictionary is not updated in session

  flask, python, session

I have a cart and a request handler that fires when the user clicks on ‘Add to cart’.

@shop_blueprint.route('product_cart/<int:product_id>/add_on_cart', methods=['GET', 'POST'])
def product_cart(product_id): 
    product = services.get_product(product_id)

    if 'cart' in session:
        if not any(product.id in d for d in session['cart']):
            session['cart'].append({product.id: 1})

        elif any(product.id in d for d in session['cart']):
            for d in session['cart']:
                d.update((k, 1) for k, v in d.items() if k == product.id)
    else:
        session['cart'] = [{product.id: 1}]

    return {'status':200}

But from this function only works:

    else:
        session['cart'] = [{product.id: 1}]

    return {'status':200}

This block fires inside a function. That is, I tried to display through a print, it shows that the product has been added to the cart.

    if 'cart' in session:
        if not any(product.id in d for d in session['cart']):
            session['cart'].append({product.id: 1})

        elif any(product.id in d for d in session['cart']):
            for d in session['cart']:
                d.update((k, 1) for k, v in d.items() if k == product.id)

But when I try to print products from the cart, in another function it always shows only 1 product.
What is my problem?

Source: Python Questions

LEAVE A COMMENT