Discover how tiny bits of data called cookies make your web experience smooth and personal!
How cookies work in HTTP in Flask - Why You Should Know This
Imagine you visit an online store and add items to your cart. Now, every time you click to view another page, the website forgets what you added because it treats each page as a brand new visit.
Without cookies, the website has no easy way to remember your choices between pages. Developers would have to pass data manually through URLs or forms, which is slow, clunky, and can expose private info.
Cookies let the website store small pieces of information on your browser. This way, the server can recognize you on each request and remember your cart or login status automatically.
response = make_response(render_template('page.html')) # No way to remember user data between requests
response = make_response(render_template('page.html')) response.set_cookie('cart', 'item1,item2')
Cookies enable websites to create smooth, personalized experiences by remembering you across multiple visits and pages.
When you log into a social media site, cookies keep you logged in as you browse different pages without asking for your password again.
Cookies store small data on the user's browser to remember information.
They solve the problem of stateless HTTP by linking requests to a user session.
This makes websites more user-friendly and interactive.
Practice
Solution
Step 1: Understand what cookies do in HTTP
Cookies store small data on the user's browser to remember information between requests.Step 2: Identify Flask's use of cookies
Flask uses cookies to save user preferences or login info on the client side.Final Answer:
To store small pieces of data on the user's browser for later use -> Option AQuick Check:
Cookies store data on browser = D [OK]
- Thinking cookies speed up the server
- Confusing cookies with encryption
- Assuming cookies manage databases
Solution
Step 1: Recall Flask's cookie setting method
Cookies are set on the response object, not the request.Step 2: Identify the correct method
The method to set a cookie isset_cookie()on the response object.Final Answer:
response.set_cookie() -> Option CQuick Check:
Set cookie on response = C [OK]
- Using request.set_cookie() instead of response.set_cookie()
- Confusing request.cookies() as a setter
- Using response.cookies() which does not exist
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
user = request.cookies.get('user')
return f'Hello, {user}!'Solution
Step 1: Understand how Flask reads cookies
The code usesrequest.cookies.get('user')to get the cookie value named 'user'.Step 2: Check the cookie value sent by client
The client sends a cookie 'user' with value 'Alice', souservariable will be 'Alice'.Final Answer:
Hello, Alice! -> Option AQuick Check:
Cookie 'user' = Alice, output = A [OK]
- Assuming cookie value is None if present
- Forgetting to use .get() method
- Confusing cookie name with literal string
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
response = 'Hello'
response.set_cookie('theme', 'dark')
return responseSolution
Step 1: Identify the response object type
The variableresponseis a string, not a Flask response object.Step 2: Understand how to set cookies in Flask
Cookies can only be set on a response object, like one created bymake_response(), not on a plain string.Final Answer:
You cannot set cookies on a string response object -> Option BQuick Check:
Set cookies on response object, not string = B [OK]
- Trying to set cookies on string instead of response object
- Thinking cookie names must be special
- Believing set_cookie needs extra arguments always
Solution
Step 1: Identify how to create a response object
Usemake_response()to create a response object to set cookies.Step 2: Set cookie with expiration
Useset_cookie()withmax_age=86400(seconds in 1 day) to set expiration.Step 3: Check each option
from flask import make_response @app.route('/') def index(): resp = make_response('Welcome') resp.set_cookie('visited', 'yes', max_age=86400) return resp correctly usesmake_responseandset_cookiewithmax_age. Others either set cookie on string or request or use wrong syntax.Final Answer:
resp.set_cookie('visited', 'yes', max_age=86400) -> Option DQuick Check:
Use make_response + set_cookie + max_age = A [OK]
- Setting cookie on string instead of response object
- Trying to set cookie on request object
- Using resp.cookies dict instead of set_cookie method
Ready to prove it?
Pass the test and this pattern moves unknown
