使用Flask在cookie中存储URL和令牌

问题描述 投票:0回答:1

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

在PHP中我可以做类似的事情:

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

在Flask中有类似的方法吗?

php html web flask
1个回答
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简介

© www.soinside.com 2019 - 2024. All rights reserved.