Flask-Cookie
Published:
This post covers cookie in Flask!
Cookie
- A piece of data that server sets in the browser
- Browser sends the request for webpage
- Server sends webpage and one or more cookie
<!-- templates/index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello Cookie</title>
</head>
<body>
<h1>Welcome to My First Page </h1>
</body>
</html>
# set_cookie.py
from flask import *
app = Flask(__name__)
@app.route('/')
def index():
page = render_template('index.html')
response = make_response(page)
response.set_cookie('userID', 'Admin')
return response
if __name__ == '__main__':
app.run(debug = True)
- Chrome -> Preferences -> Privacy and Security -> Cookies and other site data -> See all cookies and site data -> Search for localhost -> click arrow to see name and content
# cookie.py
from flask import *
app = Flask(__name__)
@app.route('/')
def index():
userID = request.cookies.get('userID')
page = render_template('index.html', name=userID)
return page
if __name__ == '__main__':
app.run(debug = True)