【问题标题】:python flask session not updatingpython烧瓶会话不更新
【发布时间】:2021-08-01 23:49:12
【问题描述】:

我有一个烧瓶应用程序,我正在通过 fetch api 向购物车添加/删除内容。当我尝试删除数据时,没有任何内容被删除。

@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'])

在我在 else 语句中打印会话后,该项目已被删除。但是,当我刷新页面或导航到另一个页面时。购物车仍然包含从会话中删除的项目。

【问题讨论】:

    标签: python-3.x flask


    【解决方案1】:

    我也遇到了this issue(已解决)。当您从购物车中更新或删除商品时,您需要将其写入会话。就我而言,是在我更新了add_product 字典中的数量之后(在cart_products 字典列表中):

    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.
    

    根据您的问题,我不知道您将把它写到哪里,但它会是这样的:

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

    或者在 .remove() 行之后试试这个:session.modified = True

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 2018-08-18
      • 2014-11-22
      • 2015-12-30
      相关资源
      最近更新 更多