0
0
Djangoframework~10 mins

Setting and getting session data in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Setting and getting session data
Request received
Access session object
Set session key-value
Get session value by key
Use value in response
Send response back
This flow shows how a Django view sets a value in the session and then retrieves it to use in the response.
Execution Sample
Django
from django.http import HttpResponse

def view(request):
    request.session['color'] = 'blue'
    favorite = request.session.get('color', 'red')
    return HttpResponse(f"Favorite color is {favorite}")
This Django view sets a session key 'color' to 'blue' and then gets it to display in the response.
Execution Table
StepActionSession StateValue RetrievedResponse Content
1Request received, session empty{}N/AN/A
2Set 'color' = 'blue' in session{'color': 'blue'}N/AN/A
3Get 'color' from session{'color': 'blue'}'blue'N/A
4Create response with favorite color{'color': 'blue'}'blue'"Favorite color is blue"
5Send response back{'color': 'blue'}'blue'"Favorite color is blue"
💡 Response sent with session data set and retrieved successfully.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
request.session{}{'color': 'blue'}{'color': 'blue'}{'color': 'blue'}
favoriteN/AN/A'blue''blue'
Key Moments - 2 Insights
Why do we use request.session.get('color', 'red') instead of request.session['color']?
Using get() avoids errors if 'color' is not set in the session. It returns 'red' as a default instead of crashing. See step 3 in execution_table where get() safely retrieves the value.
Does setting request.session['color'] = 'blue' immediately save data to the browser?
No, Django saves session data when the response is sent. The session dictionary updates in memory first (step 2), then persists after response (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the session state after step 2?
A{"color": "blue"}
B{}
C{"color": "red"}
DN/A
💡 Hint
Check the 'Session State' column at step 2 in the execution_table.
At which step is the session value 'color' retrieved?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Get 'color' from session' action in the execution_table.
If we change the default in get() to 'green', what will be the response content at step 4 if 'color' was not set?
A"Favorite color is blue"
B"Favorite color is red"
C"Favorite color is green"
D"Favorite color is null"
💡 Hint
Refer to the 'Value Retrieved' and 'Response Content' columns and imagine 'color' missing in session.
Concept Snapshot
Django sessions store data per user.
Set data: request.session['key'] = value
Get data safely: request.session.get('key', default)
Data persists across requests.
Session saves when response is sent.
Full Transcript
This example shows how Django handles session data in a view. When a request comes in, the session starts empty. The code sets a key 'color' to 'blue' in the session dictionary. Then it retrieves this value safely using get(), which returns 'blue'. Finally, the response includes the favorite color. The session data is saved after the response is sent, so it is available in future requests. Using get() prevents errors if the key is missing by providing a default value.