V
V
vfvnvsyevsky2015-03-18 00:22:19
Django
vfvnvsyevsky, 2015-03-18 00:22:19

How to update the dictionary?

There is the following representation:

def shopping_cart_add(request):
  selected_item = request.GET.get('selected_item')
  if request.session.get('cart', False):
    request.session['cart'].extend(selected_item)
  else:
    request.session['cart'] = []
    request.session['cart'].extend(selected_item)

It is expected that each extend will add a new value to the dictionary: [1,2,3,4]
But it just adds a value without a comma to the end and each time it just changes it to a new one: it was 1, add 2 will be 12, add 3 will be 13 ID How to make it as expected?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2015-03-18
@Quirel

request.session['cart'].extend(selected_item)
This code does not add anything to the dictionary, this code adds the elements from 'selected_item' to the dictionary element with the key 'cart'.
Explain what exactly you want? Still, add a new element to the dictionary , or add a value to the element that is in the dictionary ? What is the element with the 'cart' key (as I understand it, this is a list)? What is the 'selected_item' object (also a list?)?

L
lololololo, 2015-03-18
@lololololo

if request.session.get('cart', False):
if 'cart' in request.session:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question