如何在Flask中向会话添加多个项目 [英] How to add more than item to the session in Flask

查看:77
本文介绍了如何在Flask中向会话添加多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网站上创建了购物车,并且效果很好.

I've created a add-to-cart in my website and it works just great.

Am使用Jquery.getJSON发出请求以获取所选产品的价值,这是代码:

Am using Jquery.getJSON to make the request to get the value of the chosen product, here is the code:

    $(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;
      });
    });

这是接收请求的功能:

@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")

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

and here i have the template that shows all the products:

{% if entries_menu %}
    {% for menu_ent in entries_menu %}
        <div class="col-sm-6 col-md-3 col-lg-3 {{menu_ent.categorie}}">
            <div class="portfolio-item">
                <div class="hover-bg">
                    <a id="process_menu">
                        <div class="hover-text">
                            <h4 id="title">{{menu_ent.title}}</h4>
                            <small id="price">{{menu_ent.price}}</small>
                            <div class="clearfix"></div>
                            <i class="fa fa-plus add_basket"></i>
                            <div class="counter-order">
                                <div class="row">
                                    <div class="col-md-3">
                                        <form>
                                            <fieldset>
                                                <div class="form-group">
                                                    <input id="kale" name="kalkal" class="form-control" type="number" value="1" min="1" max="999" />
                                                </div>
                                            </fieldset>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <img src="../../static/{{menu_ent.path}}" class="img-responsive" alt="..." id="image-menu">
                    </a>
                </div>
                <h4 class="brand bold">{{menu_ent.title}}</h4>
                <span class="price pull-right">{{menu_ent.price}} </span><span class="amount pull-right"> {{menu_ent.amount}}</span>
                <p id="descr">{{menu_ent.descr}}</p>
            </div>
        </div>
    {% endfor %}
{% endif %}

在这里,我执行了此功能,将所有产品放入数组中,并在标题旁边的购物车上方显示这些产品,这是所有逻辑发生的文件,也应显示我想要的内容:

here i made this function to put all the products within array and showing them above where the cart exist beside the header, this is the file where all the logic happens also where it should show what i want:

        {% if session.logged_in %}

            {% if session.theOrder %}
                <div class="confirm-cancel">
                    <a href="{{url_for('flush')}}"><i class="fa fa-close fa-3x btn-danger"></i></a>
                    <a href="{{url_for('order_dish')}}"><i class="fa fa-check fa-3x btn-success"></i></a>
                </div>
            {% endif %}

            <li class='main'><a href='/#main'><span>Main</span></a></li>
            <li class="comfort"><a href='/#ckidki' ><span>Chief</span></a></li>
            <li class="menu"><a href='/menu/' ><span>Menu</span></a></li>
            <li class="order"><a href="/order/" ><span>Make an order</span></a></li>
            <li class="contact"><a href='/#contact' ><span>Contact us</span></a></li>
            <li class="last orders"><a href='/admin/' ><span>Admin</span></a></li>

            {% if session.theOrder %}

                <li class="basket"><i class="fa fa-shopping-basket fa-3x" style="color: #fff;"><p class="pull-left" id="bask" style="font-size: 19px; font-weight: 600; font-family: "Russo One",sans-serif;">{{session.get('theOrder')}} &nbspx{{session.get('price')}}</p></i></li>

            {% else %}

                <li class="basket"><i class="fa fa-shopping-basket fa-3x" style="color: #fff;"><p class="pull-left" id="bask" style="font-size: 19px; font-weight: 600; font-family: "Russo One",sans-serif;">0$ &nbsp&nbsp</p></i></li>

            {% endif %}
        {% endif %}

问题是我无法获得在会话中选择的所有产品,因此,如果我检查产品列表,我只会看到一个,实际上,每次都只能得到被点击的项目.

The problem is i can't get all the products that i've been chosen inside the session, so if i checked the products list i see only one, in fact , am getting only the clicked item every time.

请提出任何建议,无论如何,任何帮助都将不胜感激.

Please, any suggestion how to make that work, anyway please, any help would be tons appreciated .

推荐答案

我这样修改了您的代码:

I modified your code like this:

    the_order_list = session['theOrder']
    sum_list = session['price']

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

    session['theOrder'] = the_order_list
    session['price'] = sum_list

这篇关于如何在Flask中向会话添加多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆