Check for a cookie with Python Flask

前端 未结 2 469
醉话见心
醉话见心 2021-02-01 14:11

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 co

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 14:37

    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

提交回复
热议问题