# views.py
def like(request,option="food",restaurant = 1):
if request.is_ajax:
like = '%s_like' % str(option)
if 'restaurants' in request.session:
if restaurant not in request.session['restaurants']:
request.session['restaurants'][restaurant] = {}
x = request.session['restaurants'][restaurant].get(str(like),False)
if x:
return HttpResponse(False)
else:
request.session['restaurants'][restaurant][str(like)] = True
request.session.modified = True
else:
request.session['restaurants'] = {}
request.session.modified = True
我正在使用context_instance = RequestContext(request)
该会话变量可用,同时呈现响应。我的模板:
{% if request.session.restaurants.rest.id.food_like %}
working
{% else %}
failed
{% endif %}
我的视图会话密钥如下所示:
request.session["restaurants"][restaurant][like] = True
餐厅 ID在哪里restaurant
,like 可以是“food_like”、“service_like”、“special_like”之一。
那么我应该如何在我的模板中访问它呢?例如,如果我使用
request.session.restaurants.rest.id.food_like
它肯定行不通。