2. Which of the following is the correct way to set a session lifetime of 30 minutes in Flask?
easy
A. app.permanent_session_lifetime = timedelta(minutes=30)
B. session.lifetime = 30
C. app.session_lifetime = 30
D. session.permanent_session_lifetime = 30
Solution
Step 1: Identify the correct Flask attribute for session lifetime
The Flask app uses app.permanent_session_lifetime to set how long permanent sessions last.
Step 2: Use the correct data type
This value must be a timedelta object from Python's datetime module, e.g., timedelta(minutes=30).
Final Answer:
app.permanent_session_lifetime = timedelta(minutes=30) -> Option A
Quick Check:
Use app.permanent_session_lifetime with timedelta [OK]
Hint: Use app.permanent_session_lifetime with timedelta object [OK]
Common Mistakes:
Setting session.lifetime instead of app.permanent_session_lifetime
Assigning integer instead of timedelta
Using session.permanent_session_lifetime which does not exist
3. Given this Flask code snippet, what will be the session lifetime if the user sets session.permanent = True and app.permanent_session_lifetime = timedelta(hours=1)?
5. You want to create a Flask app where sessions last 2 hours, but only if the user chooses 'Remember me'. Otherwise, sessions should expire when the browser closes. Which code snippet correctly implements this?