# 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
ここrestaurant
でレストランIDはどこにあり、likeは "" food_like "、" service_like "、"special_like"のいずれかになります。
では、テンプレートでどのようにアクセスするのですか?たとえば、私が使用する場合
request.session.restaurants.rest.id.food_like
それは確かに機能しません。