def like(request,option="food",restaurant = 1):
if request.is_ajax:
rest = 'rest'
like = '%s-like' % str(option)
if restaurant in request.session:
if like not in request.session[restaurant]:
request.session[restaurant][like] = str(like)
else:
return HttpResponse('warning')
if restaurant not in request.session:
request.session[restaurant] = restaurant
r = Restaurant.objects.get(pk=restaurant)
if option == "food":
r.food_likes +=1
r.save()
return HttpResponse(str(r.food_likes))
elif option=="service":
r.service_likes +=1
r.save()
return HttpResponse(str(r.service_likes))
elif option == "ambience":
r.ambience_likes +=1
r.save()
return HttpResponse(str(r.ambience_likes))
else:
return redirect(request.META.get('HTTP_REFERER'))
'unicode' object does not support item assignment
request.session[restaurant][like] = str(like)
我在使用变量字符串设置会话密钥时出现问题, 我该怎么设置会话密钥'喜欢'里面的'餐厅'键?,请帮忙
答案 0 :(得分:0)
首先创建一个空字典
request.session[restaurant] = dict()
然后完成作业
request.session[restaurant][like] = str(like)