问题
I would like to get a cookie (e.g. country
) with this Flask call.
data = request.cookies.get("country")
How can I tell if the cookie exists?
回答1:
request.cookies
is a dict
, so:
if 'country' in request.cookies:
# do something
else:
# do something else
回答2:
request.cookies.get('my_cookie')
should have worked. If it didn't work, you may not have access to the request object when you call this line.
Try importing flask at the top
import flask
then call
cookie = flask.request.cookies.get('my_cookie')
If the cookies exists, it will get assigned to cookie
and if not then cookie
will equal None
来源:https://stackoverflow.com/questions/13531149/check-for-a-cookie-with-python-flask