the_martian
the_martian

Reputation: 634

Storing a URL and token in a cookie using Flask

I trying to pick up Flask and I'm having trouble finding some information on what I'm trying to do. I want to incorporate the flask code into HTML and use it to store a URL and Token in a cookie.

In PHP I could do something like:

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

Is there a similar way of doing it in Flask?

Upvotes: 0

Views: 177

Answers (1)

Alex
Alex

Reputation: 237

basically, in Flask , cookie is stored as dict{'key': value} and set the cookie in response. For example:

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

then you cant get cookie like this:

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

or in HTML: url.html

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

return the html template:

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

Here is brief of cookie in Flask Documentation Flask Quickstart

Upvotes: 1

Related Questions