Search code examples
djangosession-variablesdjango-sessions

Testing a session variable


I came across Django request.session; 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?

Is there a way to check it?


Solution

  • get will do all the work for you. Check if the key exists, if not then the value is None

    if request.session.get('name', None) == "dummy":
        print 'name is = dummy'