开发者

I'm adding data into array but, it's end up with only the last item in the array. It seems to overwrite

开发者 https://www.devze.com 2023-01-16 05:52 出处:网络
request.session[\'list\'] = [] if request.开发者_StackOverflow中文版method ==\'POST\': newrecord = request.POST[\'market\']
request.session['list'] = []
if request.开发者_StackOverflow中文版method =='POST':    
  newrecord = request.POST['market']
  tmp = request.session['list']
  tmp.append(newrecord)
  request.session['market_list'] = tmp

I turn out that previous data was overwrite by the new one


You are assigning an empty list to request.session['list'] in the first line of the code snippet you have given. Is this by design? In that case it is no surprise that tmp always ends up with one element only.


change request.session['list'] = [] to

if not request.session.has_key('list'):     
  request.session['list'] = []
0

精彩评论

暂无评论...
验证码 换一张
取 消