A
A
Andrey Voskresensky2016-04-08 09:34:28
Django
Andrey Voskresensky, 2016-04-08 09:34:28

How to display dictionary values ​​from a list of Python/Django session objects?

I need to add and withdraw objects from the session in such a way that I can then display the characteristics of the displayed objects!
There is a session with a list that has dictionaries -

cart(переменная сессии) [{'price': 436, 'product_id': 5}, {'price': 436, 'product_id': 6}]

I add them in the following way
views.py
def add_product_to_cart(request):
  if request.method == 'POST':
    id = int(request.POST['product_id'])
    price = int(request.POST['price'])
    if 'cart' not in request.session:
      request.session['cart'] = []
    request.session['cart'].append(dict({'product_id': id, 'price': price}))
    request.session.modified = True
    return redirect("/")

and of course I need to display the values ​​of the 'product_id' and 'price' keys in the heatmap. I'm trying to do it like this:
def show_cart(request):
  cart = request.session.get('cart')
  product_items = Product.objects.filter(id__in=cart) #пробовал и так Product.objects.filter(id__in=request.session['cart'])
  content={
    "product_items":product_items
  }
  return render (request, "orderdetail.html", content)

as a result I get an error - int() argument must be a string, a bytes-like object or a number, not 'dict'
Maybe someone has ideas how to implement this?
originally did this:
def add_product_to_cart(request, id):
  if 'cart' not in request.session:
    request.session['cart'] = []
  request.session['cart'].append(int(id))
  request.session.modified = True
  return redirect("/")

def show_cart(request):
  cart = request.session.get('cart') 
  product_items = Product.objects.filter(id__in=cart)
  content={
    "product_items":product_items
  }
  return render (request, "orderdetail.html", content)

received only the id of the object, but as a result received only the name of the product!! I couldn't get any other data!
THANKS IN ADVANCE!!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
iegor, 2016-04-08
@Voskresenskyi

Replace with id__in=(x['price'] for x in cart)

A
Andrey Voskresensky, 2016-04-08
@Voskresenskyi

My answer is:
views.py

def add_product_to_cart(request):
  if request.method == 'POST':
    id = int(request.POST['product_id'])
    if 'cart' not in request.session:
      request.session['cart'] = []
    request.session['cart'].append(int(id))
    request.session.modified = True
    return redirect("/")

def show_cart(request):
  cart = request.session.get('cart')
  product_items = Product.objects.filter(id__in=cart) #можно и так Product.objects.filter(id__in=request.session['cart'])
  content={
    "product_items":product_items
  }
  return render (request, "orderdetail.html", content)

def add_product_to_cart(request):
  if request.method == 'POST':
    id = int(request.POST['product_id'])
    price = int(request.POST['price'])
    if 'cart' not in request.session:
      request.session['cart'] = []
    request.session['cart'].append(dict({'product_id': id, 'price': price}))
    request.session.modified = True
    return redirect("/")

def show_cart(request):
  cart = request.session.get('cart')
  product_items = Product.objects.filter(id__in=(x['product_id'] for x in cart) )#можно прописать и для price вот только фактически это не нужно...
  content={
    "product_items":product_items
  }
  return render (request, "orderdetail.html", content)

In addition - read your code! In this case, I missed the bracket in the template (probably accidentally deleted it)))
Thanks iegor for the help,)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question