using django session inside templates
Solution 1
You might not have django.core.context_processors.request
in your settings.TEMPLATE_CONTEXT_PROCESSORS
.
You can try to print {{ request }}
in the template, if it shows nothing then you don't have it.
You can also check it with ./manage.py shell:
from django.conf import settings
print settings.TEMPLATE_CONTEXT_PROCESSORS
If django.core.context_processors.request
is not there, then copy TEMPLATE_CONTEXT_PROCESSORS
from the shell output into your settings.py, and add django.core.context_processors.request
to this list.
Solution 2
Complementing @jpic response.
Instead of copying the contents TEMPLATE_CONTEXT_PROCESSORS from shell you can do the following:
from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
"django.core.context_processors.request",
)
In this way, your global settings will be preserved.
Make sure to keep the trailing comma so python could treat this as a tuple
Related videos on Youtube
Abhimanyu
Updated on September 24, 2022Comments
-
Abhimanyu over 1 year
# 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
I am using
context_instance = RequestContext(request)
so that session variable is available, while rendering response. My template:{% if request.session.restaurants.rest.id.food_like %} working {% else %} failed {% endif %}
My view session key looks like this :
request.session["restaurants"][restaurant][like] = True
where
restaurant
is the restaurant id, and like could be one of " "food_like", "service_like", "special_like".So how am i supposed to access it in my templates? For example if i use
request.session.restaurants.rest.id.food_like
it won't work for sure.
-
Abhimanyu over 11 yearsyea its not there , so how am i supposed to include it , in my settings file from django.conf import settings print settings.TEMPLATE_CONTEXT_PROCESSORS and then to my TEMPLATE_CONTEXT_PROCESSORS add "django.core.context_processors.request" ??
-
jpic over 11 yearsIf "django.core.context_processors.request" is not there, then copy TEMPLATE_CONTEXT_PROCESSORS from the shell output into your settings.py, and add "django.core.context_processors.request" to this list.
-
Abhimanyu over 11 yearsbut one more doubt , how am i supposed to give a dynamic value , for example it wont accept , rest.id as i have listing of many instances of same model , so how am i supposed to overcome that problem ,
-
jpic over 11 yearsI really don't understand, could you open another topic with more details ? thanks !
-
jpic over 11 yearsjust print {{ request.session }} it will show you the structure. My guess is that {{ request.session.restaurants.rest }} blocks. Try instead: {% for key, value in request.session.restaurants.items %}{{ key }}, {{ value }}{% endfor %} and descend from the foodchain like this.
-
James about 9 yearsYou really helped me out here by pointing out how do this in the shell. +1