0

I'm setting the session value for a variable in my as follows

request.session['a'] = True
request.session.set_expiry(604800)

If I access the session in my template as follows, it shows it's value

{{ request.session.a }}

How can I get the session expiry date for this in my template?

I tried {{ request.session.a.get_expiry_date }} and {{ request.session.a.expire_date }} but both did not work.

Yin Yang
  • 1,776
  • 3
  • 26
  • 48
  • you are setting the property as a boolean. It naturally would not have a key `get_expiry_date` in it. Did you try just `request.session.get_expiry_date` ? – karthikr Oct 01 '14 at 16:40
  • @karthikr I'm also setting the expiry as follows `request.session.set_expiry(604800)` – Yin Yang Oct 01 '14 at 16:40
  • Ok.. did you try `request.session.get_expiry_date` ? (you should not be accessing property of `a` here) – karthikr Oct 01 '14 at 16:41
  • @karthikr `request.session.get_expiry_date` always evaluates to 7 days from now and if I refresh it, it changes it according to the current time. How can I access the expiry date of a particular variable? – Yin Yang Oct 01 '14 at 16:46
  • That's because you are resetting the expiration date with set_expiry in your view, so it is always going to be 7 days from now. Remove the line about `request.session.set_expiry(604800)`. – Joseph Oct 01 '14 at 17:11
  • @Joseph The session and the expiry date are being set in an AJAX view and not the main page view. That AJAX view is called only once. – Yin Yang Oct 01 '14 at 17:14
  • I would verify that the session expiry reset is only being called once. Get a browser extension to look at your cookie details and you'll see the value in your template is the value of the session cookie's expiry. Maybe throw some debug output around the line where you set the session expiry. – Joseph Oct 01 '14 at 17:18
  • @Joseph I've verified that the session expiry is not being called again. – Yin Yang Oct 01 '14 at 18:14
  • @karthikr I just noticed printing `request.session.get_expiry_date()` in the view prints the date one week from the time I set the session in my AJAX view and `{{ request.session.get_expiry_date }}` in my template prints the date one week from now. – Yin Yang Oct 01 '14 at 19:50
  • Interesting !! I was trying to come up with a clean solution. Will let you know if i come up with one. – karthikr Oct 01 '14 at 20:03

1 Answers1

2

{{ request.session.get_expiry_date }} in a template gives the session expiry date.

I also learned that

request.session['a'] = True
request.session.set_expiry(604800)

sets the expiry for the entire session and not just that key.

Matthew Purdon
  • 754
  • 11
  • 28
Yin Yang
  • 1,776
  • 3
  • 26
  • 48