How to add more than item to the session in Flask


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

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

and here is the function that receiving the request:

@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}}

</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')}}

</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

</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

I was facing the same issue and finally got this answer!

@app.route('/addtocart/') #加入购物车选项 def addtocart(): id = request.args.get('id') if 'cart' not in session: session['cart'] = [] # cart_list = session['cart'] cart_list.append(id) session['cart'] = cart_list # print(session)

cart_list = session['cart'] will return an empty list. Then, after cart_list.append(id), you've got a list with length 1. The key sentence is session['cart'] = cart_list, if you don't do this, your list's maximum length is 2.



  • c++, multiple instances of a dll, singleton
  • Cosmos Db Throughput
  • Execution failed for task Program type already present
  • Are PWA sites in same domain not able to install both on home screen?
  • Recommended hash function for overwriting NSObject's hash method
  • Emulate XMP tag with script tag and allow to copy content
  • pattern matching of the form: Option{..}
  • Get all records from azure table storage
  • Is there a way to sort the firebaserecyclerview after I calculate value after retrieving data?
  • How to get rid of green warning line in VS2010 editor?
  • Pandas Subset of a Time Series Without Resampling
  • Added String to ArrayList not shown / not updated [duplicate]
  • Custom test adapter installed via NuGet isn't discovering tests
  • expand ipv6 address in shell script
  • Condition on a timestamp column to select data for a year
  • Pass data from page to page safely
  • How to generate nodes with customized shape?
  • How to add fog to texture in shader (THREE.JS R76)
  • Antcontrib and in ant?
  • Cross-platform way of having non-blocking console input
  • Wordpress plugins it asks for FTP Details
  • Grouping via an element in hash [closed]
  • How do I get partial cell styling in excel using EPpplus?
  • How can I send data in text field using Selenium?
  • exception thrown while building the java application using netbeans
  • Was default_marker removed from mapbox-gl.js
  • Karate: JsonPath wildcards didn't work or partly didn't work
  • Returning the content of multiple files in node.js
  • How to implement tap to focus on barcode scanner app using swift?
  • Is there a command line tool to get the machine code for an assembly instruction?
  • React / Webpack - “Module parse failed: Unexpected token - You may need an appropriate loader to han
  • QNetworkAccessManager one instance and connecting slots
  • Videos won't upload
  • In metro, get all inherited classes of an (abstract) class?
  • Defer unused CSS
  • Modifying native query cannot have named parameter bindings?
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • Running R's aov() mixed effects model from Python using rpy2
  • ReferenceError: TextEncoder is not defined