Search code examples
pythondjangosessioncookiessession-variables

django set session variable till end of day


I need to set a session variable that expires at the end of each day.

I know I can set a session variable like this:

request.session['my_variable'] = 'foo'

How can I then have this expire at the end of the day?


Solution

  • You can set an entire session to expire at the end of the day with

    request.session.set_expiry(seconds_until_end_of_day)
    

    or you can set a default expiration for all session cookies in your app with the setting

    SESSION_COOKIE_AGE
    

    again in seconds. If you absolutely need to set a specific cookie to have a special expiration then you may have to look into the session backend.