Testing a session variable in django

user739811 picture user739811 · May 6, 2011 · Viewed 14.7k times · Source

I came across Django Request.Session where, I know how to set and test it for a specific value.

request.session['name'] = "dummy"

and somewhere i check

if request.session['name'] == "dummy" :
   #do something

But, now i have to check whether the session variable was even set in the first place? I mean how can i check whether there exists a value in the Request.Session['name'] Is set ?

Please can anyone tel me, is there a way to check it?

Thank you in advance.

Answer

Lars Wiegman picture Lars Wiegman · May 6, 2011

Treat it as a Python dictionary:

if 'name' in request.session:
    print request.session['name']

How to use sessions: http://docs.djangoproject.com/en/dev/topics/http/sessions/