小编Rez*_*nov的帖子

如何在 Flask 中向会话添加多个项目

我在我的网站上创建了一个“添加到购物车”,效果非常好。

我用来Jquery.getJSON发出请求以获取所选产品的价值,以下是代码:

    $(function() {
      $('a#process_menu').bind('click', function() {
        /*var amount = document.getElementById('kale').value;*/
        $.getJSON('{{url_for("get_the_order")}}', {
          menu_order: $(this).text(), price_order: $('input[name="kalkal"]').val(),
        }, function(data) {
          location.reload();
        });
        return false;
      });
    });
Run Code Online (Sandbox Code Playgroud)

这是接收请求的函数:

@app.route('/get_the_order')
def get_the_order():

    global the_order_list

    try:

        the_order_list = []
        sum_list = []

        get_order = request.args.get('menu_order', 0, type=str)
        get_price = request.args.get('price_order', 0, type=str)

        the_order_list.append(get_order)
        sum_list.append(get_price)

        session['theOrder'] = ' '.join(the_order_list)
        session['price'] = ' '.join(sum_list)

        return jsonify(result=the_order_list + sum_list)

    except Exception as e:
        return redirect("menu")
Run Code Online (Sandbox Code Playgroud)

这里我有显示所有产品的模板:

{% if entries_menu %}
    {% for menu_ent in …
Run Code Online (Sandbox Code Playgroud)

python jinja2 flask

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

flask ×1

jinja2 ×1

python ×1