22. Cookie expiration

Many site cookies are classified as 'session cookies,' which expire when the browser is closed. However, 'persistent cookies' are designed to persist beyond browser sessions.

The management of cookies and user sessions in Django is handled by the MIDDLEWARE section of the settings.py file. To ensure that cookie data persists after the browser is closed, we can set an expiration time.

In the set_cookie command within the add_to_cart function, we will include the max_age parameter to determine the duration in seconds for which the cookie will remain active after its creation. Specifically, we will set an expiration period of 40 days.

views.py
answer.set_cookie(key="id_session", value=id_session, max_age=60*60*24*40)

Last updated