N
N
nanny_ogg2018-08-01 11:35:45
Python
nanny_ogg, 2018-08-01 11:35:45

How can a cart be implemented using sessions in Flask?

Good afternoon. I started to learn python and flask, but some difficulties arose.
My idea is this - when you click on the "Add to Cart" button, the parameters of the product are transferred to the handler, they are written to the session, and all the products that are in the session are displayed on the basket page.
In php I would do it something like this:

$_SESSION["cart"][] = array("product_name" => "$_POST['product_name']");

But in python, I encountered the fact that I don’t know how to pass a list or dictionary to the session parameter, and I don’t know at all what and how it would be correct to pass there. At the moment, adding only one product works, it is displayed, but when adding a second product, everything is rewritten to a new product and the old one is overwritten, which is logical.
if request.method == 'POST':
        session['product_name'] = request.form['product_name']
        session['product_image'] = request.form['product_image']
        session['product_description'] = request.form['product_description']

        return render_template('cart.html', session=session)

Maybe I'm thinking in the wrong direction? How competently it would be to implement?
And another question, in php you can safely see what is in the session by calling print_r($_SESSION), is there something similar in python?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
planc, 2018-08-01
@nanny_ogg


return render_template('cart.html', session=session)
this is not your session, but a variable in the template
flask-russian-docs.readthedocs.io/ru/latest/quicks...
https://habr.com/post/346306/
And another question, in php you can safely see what is in the session by calling print_r($_SESSION), is there something similar in python?
dir(obj), vars(obj)
https://docs.python.org/3/library/pdb.html
import pdb; pdb.set_trace()

K
Kolya K, 2015-01-02
@Kolyagrozamorey

If you don’t want to have problems with refilling, take canon, for example, www.dns-shop.ru/catalog/i160075/printer-canon-i-se...
Their chip does not block the printer, sometimes it swears that the toner is out of toner, but it prints further

K
Konstantin Frolov, 2015-01-02
@nitro80

Recently, Ricoh printers were advertised on Habré as those that can be refilled without chips, and without loss of warranty by refilling with non-original cartridges.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question