Setting and Getting Session Data in Django
📖 Scenario: You are building a simple Django web app that tracks a user's favorite color during their visit. You want to store this color in the session so it remembers the choice as the user navigates pages.
🎯 Goal: Build a Django view that sets a session variable called favorite_color to a specific value, and another view that reads and displays this session value.
📋 What You'll Learn
Create a Django view function called
set_color that sets request.session['favorite_color'] to the string 'blue'.Create a Django view function called
get_color that reads request.session['favorite_color'] and returns it in an HttpResponse.Use the Django
HttpResponse class to return simple text responses.Ensure the session data is properly set and retrieved using Django's session framework.
💡 Why This Matters
🌍 Real World
Web apps often need to remember user preferences or temporary data during a visit. Sessions let you store this data securely on the server side.
💼 Career
Understanding session management is essential for backend web developers working with Django or similar frameworks to maintain user state and preferences.
Progress0 / 4 steps