2

视图.py

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)

怀疑

我在使用变量字符串设置会话密钥时遇到问题,我应该如何在“餐厅”键中设置会话密钥“like”??请帮忙

4

1 回答 1

0

首先创建一个空字典 request.session[restaurant] = dict()

然后做作业 request.session[restaurant][like] = str(like)

于 2012-08-23T06:54:55.053 回答