如何使用JQuery和Flask更新字典中的特定值?

如何使用JQuery和Flask更新字典中的特定值?,flask,Flask,我已经创建了一个包含我的产品的会话列表,我需要通过增加产品数量来更新任何产品的数量,因为我使用了HTMLtype=“number”,我还创建了一个函数,将更改的数量乘以当前数量,因此,假设默认情况下第一个产品的数量为2通过增加数字,假设2,产品数量将变为4,依此类推,价格也将成倍增加 代码如下: <th style="text-align: center;" class="amount-izd">{{value["amount"]}}</th> <th style

我已经创建了一个包含我的产品的会话列表,我需要通过增加产品数量来更新任何产品的数量,因为我使用了
HTML
type=“number”,我还创建了一个函数,将更改的数量乘以当前数量,因此,假设默认情况下第一个产品的数量为2通过增加数字,假设2,产品数量将变为4,依此类推,价格也将成倍增加

代码如下:

<th style="text-align: center;" class="amount-izd">{{value["amount"]}}</th>

<th style="text-align: center; width: 14%;">
    <div class="block">

        <input type="number" id="myNumber" value="1" min=1 data-amount='{{value["amount"]}}' data-pros='{{value["id"]}}' data-price='
        {% if g.currency == "euro" %} 
            {{format_price(value["price"] * config.SITE_CURRENCIES["euro"]).rsplit(".",1)[0]}}
        {% elif g.currency == "dollar" %} 
            {{format_price(value["price"] * config.SITE_CURRENCIES["dollar"]).rsplit(".",1)[0]}}
        {% else %} 
            {{format_price(value["price"] * config.SITE_CURRENCIES["ruble"]).rsplit(".",1)[0]}}
        {% endif %}
        '>
        <label for="myNumber">qty</label>

    </div>
</th>
最后是视图.py:

@profile_route.route("/standard-<set_curr>/profile/cart/", methods=['GET','POST'])
@authorize
def cart_products():

    if "cart" not in session:
        return render_template("my-cart.html", display_cart = {}, total = 0)

    else:
        items = session["cart"]
        dict_of_products = {}
        total_price = 0
        for item in items:
            product = Goods.query.get(item)
            total_price += product.price
            if product.id in dict_of_products:
                pass
            else:
                dict_of_products[product.id] = {"qty":1, "name":product.product_name, 'category':product.Category.name, "sizes": product.sizes, "hex_color":product.hex_color,  "text_color":product.text_color, "material":product.material, "article":product.article, "price":product.price, "sort": product.sort, "amount": product.amount, 'slug':product.slug, 'public_id' : product.public_id, "id":product.id}

    return render_template("my-cart.html", display_cart=dict_of_products, total = total_price)

@profile_route.route("/standard-<set_curr>/profile/<int:id>/update/price/<price>", methods=['GET','POST'])
@login_required
def update_price(id, price):

    items = session["cart"]
    dict_of_products = {}

    for item in items:
        product = Goods.query.get(item)
        if product.id in dict_of_products:
            dict_of_products[id]['price'] = price
            return jsonify(success=dict_of_products[id]['price'])
    return jsonify(error='No product found.')
@profile\u route.route(“/standard-/profile/cart/”,方法=['GET','POST'])
@授权
def cart_产品():
如果“购物车”不在会话中:
返回render_模板(“my cart.html”,display_cart={},total=0)
其他:
项目=会话[“购物车”]
dict_of_乘积={}
总价=0
对于项目中的项目:
product=货物.query.get(项目)
总价+=产品价格
如果产品目录中的product.id:
通过
其他:
产品目录[product.id]={“数量”:1,“名称”:product.product\u名称,“类别”:product.category.name,“尺寸”:product.size,“十六进制颜色”:product.hex\u颜色,“文本颜色”:product.text\u颜色,“材料”:product.material,“物品”:product.article,“价格”:product.price,“排序”:product.sort,“金额”:product.amount,“slug”:product.slug,“public\u id”:product.public\u id,“id”:product.id}
返回渲染模板(“my cart.html”,display\u cart=dict\u of\u products,total=total\u price)
@profile_route.route(“/standard-/profile//update/price/”,方法=['GET','POST']))
@需要登录
def更新价格(id,价格):
项目=会话[“购物车”]
dict_of_乘积={}
对于项目中的项目:
product=货物.query.get(项目)
如果产品目录中的product.id:
产品目录[id][“价格”]=价格
return jsonify(success=dict\u of_products[id]['price'])
返回jsonify(错误=“未找到任何产品”。)
如果我更改了金额,在控制台中我得到一个
500错误
,上面写着:

return jsonify(success=dict\u of_products[id]['price'])
键错误:47

请问如何克服这个问题

更新


我想知道,是否可以通过直接从
JQuery
访问字典来更新字典的任何值?

没有答案的家伙!!有什么答案吗!??请回答任何问题!!
@profile_route.route("/standard-<set_curr>/profile/cart/", methods=['GET','POST'])
@authorize
def cart_products():

    if "cart" not in session:
        return render_template("my-cart.html", display_cart = {}, total = 0)

    else:
        items = session["cart"]
        dict_of_products = {}
        total_price = 0
        for item in items:
            product = Goods.query.get(item)
            total_price += product.price
            if product.id in dict_of_products:
                pass
            else:
                dict_of_products[product.id] = {"qty":1, "name":product.product_name, 'category':product.Category.name, "sizes": product.sizes, "hex_color":product.hex_color,  "text_color":product.text_color, "material":product.material, "article":product.article, "price":product.price, "sort": product.sort, "amount": product.amount, 'slug':product.slug, 'public_id' : product.public_id, "id":product.id}

    return render_template("my-cart.html", display_cart=dict_of_products, total = total_price)

@profile_route.route("/standard-<set_curr>/profile/<int:id>/update/price/<price>", methods=['GET','POST'])
@login_required
def update_price(id, price):

    items = session["cart"]
    dict_of_products = {}

    for item in items:
        product = Goods.query.get(item)
        if product.id in dict_of_products:
            dict_of_products[id]['price'] = price
            return jsonify(success=dict_of_products[id]['price'])
    return jsonify(error='No product found.')