using django session inside templates

11,269

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_PROCESSORSfrom 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

Share:
11,269

Related videos on Youtube

Abhimanyu
Author by

Abhimanyu

Updated on September 24, 2022

Comments

  • Abhimanyu
    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
    Abhimanyu over 11 years
    yea 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
    jpic over 11 years
    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.
  • Abhimanyu
    Abhimanyu over 11 years
    but 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
    jpic over 11 years
    I really don't understand, could you open another topic with more details ? thanks !
  • jpic
    jpic over 11 years
    just 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
    James about 9 years
    You really helped me out here by pointing out how do this in the shell. +1