OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to add and remove sessions from jinja template? or eventually how to make a cart functionality?

  • Thread starter Thread starter Besmella Kazimy
  • Start date Start date
B

Besmella Kazimy

Guest
Is there any way to manage (add and remove) sessions from Jinja template without interacting with any kind of API or page reload?

Im making a cart function for a store website, and I wanted to store items as:

Code:
session['cart'][str(item.id)] = [item.name, item.item_type, item.id]

which is a dictionary inside session storing list of three values and I can access it using

Code:
{{ session.cart[item.id|string] }}

It was easy in the backend but I wonder if I can do it inside Jinja template, like this:

Code:
{% set session.cart[item.id|string] = [item.name, item.item_type, item.id] %}

is it possible?

not to mention that I want to use it with javascript event listener

If not possible, let me know what is the best way to add and remove from sessions and update the cart length in navbar without any reload nor refresh.

My initial algorithm was to make a rest API route that would add and remove from the cart session as mentioned above, but I couldn't make it work, and here is the cart route code:

Code:
@app.route('/cart/api')
def cart_api():
    data = flask.request.args
    data = [data['nm'], data['typ'], data['id']]
    if flask.request.args.get('act') == '1':
        flask.session['cart'][data[2]] = data
        return f"{data[0]} added to cart successfuly."
    
    else:
        flask.session['cart'].remove(data[2])
        return f"{data[0]} was not found in session.", 404

when I use the API only and send the following request, it will return a 200 status code and say that item added to cart as it should, but there is no update on the main page where I have my website

Code:
HTTP://mydomain/cart/api?nm=abc&typ=alphabet&id=6&act=1

the response:
abc added to cart successfully.

<p>Is there any way to manage (add and remove) <code>sessions</code> from <code>Jinja</code> template without interacting with any kind of <code>API</code> or page reload?</p>
<p>Im making a <code>cart function</code> for a store website, and I wanted to store items as:</p>
<pre><code>session['cart'][str(item.id)] = [item.name, item.item_type, item.id]
</code></pre>
<p>which is a <code>dictionary</code> inside <code>session</code> storing <code>list</code> of three values and I can access it using</p>
<pre><code>{{ session.cart[item.id|string] }}
</code></pre>
<p>It was easy in the backend but I wonder if I can do it inside <code>Jinja template</code>, like this:</p>
<pre><code>{% set session.cart[item.id|string] = [item.name, item.item_type, item.id] %}
</code></pre>
<p>is it possible?</p>
<p>not to mention that I want to use it with <code>javascript event listener</code></p>
<p>If not possible, let me know what is the best way to <code>add</code> and <code>remove</code> from sessions and <code>update</code> the cart length in <code>navbar</code> without any <code>reload</code> nor <code>refresh</code>.</p>
<p>My initial algorithm was to make a rest API route that would add and remove from the cart session as mentioned above, but I couldn't make it work, and here is the cart route code:</p>
<pre><code>@app.route('/cart/api')
def cart_api():
data = flask.request.args
data = [data['nm'], data['typ'], data['id']]
if flask.request.args.get('act') == '1':
flask.session['cart'][data[2]] = data
return f"{data[0]} added to cart successfuly."

else:
flask.session['cart'].remove(data[2])
return f"{data[0]} was not found in session.", 404
</code></pre>
<p>when I use the API only and send the following request, it will return a <code>200 status code</code> and say that <code>item added to cart</code> as it should, but there is no <code>update</code> on the <code>main page</code> where I have my website</p>
<pre><code>HTTP://mydomain/cart/api?nm=abc&typ=alphabet&id=6&act=1

the response:
abc added to cart successfully.
</code></pre>
 

Latest posts

Top