【问题标题】:Storing a URL and token in a cookie using Flask使用 Flask 将 URL 和令牌存储在 cookie 中
【发布时间】:2019-03-14 00:13:13
【问题描述】:

我试图拿起 Flask,但我无法找到有关我正在尝试做的事情的一些信息。我想将烧瓶代码合并到 HTML 中,并使用它将 URL 和 Token 存储在 cookie 中。

在 PHP 中我可以这样做:

<?php system('cgi-bin/current_user.py '.$_COOKIE['api_token'].' '.$_COOKIE['api_url']);

在 Flask 中有类似的方法吗?

【问题讨论】:

    标签: php html web flask


    【解决方案1】:

    基本上,在 Flask 中,cookie 存储为 dict{'key': value} 并设置 cookie 作为响应。例如:

    @app.route('/set_cookie')  
    def set_cookie():  
        response=make_response('Hello World');  
        response.set_cookie('url','url_address_here')  
        return response
    

    那么你不能像这样得到cookie:

    @app.route('/get_cookie')  
    def get_cookie():  
        name=request.cookies.get('url')  
        return name 
    

    或在 HTML 中: url.html

    <h1>the url is {{request.cookies.get('url')}}</h1>
    

    返回html模板:

    @app.route('/get_template')  
        def get_template():  
            return render_template('url.html')   
    

    这里是 Flask 文档 Flask Quickstart 中的 cookie 简介

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 2016-09-13
      • 2021-10-07
      • 2020-07-04
      • 2021-10-16
      • 2012-11-14
      • 2019-02-14
      相关资源
      最近更新 更多