A session ID uniquely identifies an active application session. A Flask application can place it in a browser cookie, display it in a template, and use it to recover the same session after a refresh or accidental browser closure. section 6.2.1 section 6.2.2
A practical session-ID flow is:
- Open the database connection and check whether the request contains a session_id cookie. section 6.2.2
- Create a session and question set when no cookie value exists. section 6.2.2 section 6.2.3
- Set the generated ID in the response cookie and pass it to the template for display. section 6.2.1 section 6.2.2
- Pass an existing ID into the question-set method so stored questions replace newly randomized questions. section 6.2.3
Keeping a question session stable
A page can conditionally show “Session: 42” when session_id exists. On the first visit, the application creates a session and stores its ID in a cookie; later visits read that value and retrieve the associated question set. If the question method ignores the ID, every refresh generates different questions even though the displayed session remains unchanged. section 6.2.1 section 6.2.3
<h1>Ham Radio Practice Test</h1>
{% if session_id %}
<h3>Session: {{session_id}}</h3>
{% endif %}
Inspect the conditional template markup that displays a session ID only when the application supplies one. section 6.2.1
# Set the session ID as a cookie
response = make_response(render_template
('index.html', data=data, session_id=session.session_id))
response.set_cookie('session_id', str(session.session_id))
return response
Inspect the Flask response construction that renders the page and stores session_id as a cookie. section 6.2.2


