if not 'saved' in request.session or not request.session['saved']: request.session['saved'] = [obj] else: saved_list = request.session['saved'] saved_list.append(obj) request.session['saved'] = saved_list
Here is what the above code is Doing:
1. If the session doesn’t have a key called ‘saved’, it creates one and sets it to a list containing the object.
2. If the session does have a key called ‘saved’, it appends the object to the list.
3. It then saves the list back to the session.