Check for a cookie with Python Flask

女生的网名这么多〃 提交于 2020-05-09 18:48:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!