Reputation: 101
I'm trying to use a cookie to remember if a visitor has already seen a particular tutorial page on my site. The site is build using Flask.
The tutorial page gets loaded from flask routing so I thought it made sense to try and alter the cookie in the flask routing definition using the make_response and response.set_cookie function from the flask framework.
However, this (session) cookie is only for the duration of the session. I can't find any info on setting permanent/persistent cookies with flask. How can I do this with flask ?
Thanks!
Upvotes: 1
Views: 4500
Reputation: 5808
To set a persistent cookie, you must add the "expires" field in the http header:
Set-Cookie: <cookie-name>=<cookie-value>; Expires=<date>
If you don't provide Expires=
, then the browser considers the cookie as a "session" cookie and deletes the cookie when the browser is closed.
For Flask, you can use the parameter expires=
of the response.set_cookie() function like this for a 30-days cookie:
import datetime
response.set_cookie(name, value,
expires=datetime.datetime.now() + datetime.timedelta(days=30))
Upvotes: 9