1

I have a flask application and i am adding/removing content to the cart via fetch api. When i try to remove the data nothing gets removed.

@cart_bp.route('/cart/action/',methods=['POST'],strict_slashes=False)
def cart():
    fetchapi = request.get_json()
    if fetchapi['action'].lower() == 'add':
        session['cart'].append(fetchapi['item'])
    else:
        session['cart'].remove(fetchapi['item'])

After I print the session in the else statement the item has been removed. However when I refresh the page or navigate to another page. The cart still contains the item that got removed from the session.

1 Answer 1

1

I ran into this issue (solved) too. When you're updating or removing an item from the shopping cart, you need to write that to the session. In my case, it was after I updated the quantity in the add_product dictionary (within cart_products list of dictionaries):

cart_products = [add_product if product['prod_id'] == add_product['prod_id'] else product for product in cart_products]
session["cart"] = cart_products # Without this, my dictionary was updated, but the updated quantity wasn't showing in the cart.

I don't know the name of where you'd write this to based on your question as is, but it would be something like:

session['cart'].remove(fetchapi['item'])
session["cart"] = fetchapi

Or try this after .remove() line: session.modified = True

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.