Django Session
What is Django Session?
Imagine when you visit a website, your computer talks to the website’s computer using a language called HTTP. This language is a bit forgetful; it doesn’t remember past conversations. So, if a website wants to remember things about you, like your preferences or if you’re logged in, it needs a way to do that.
That’s where sessions come in. In simple terms, sessions are like a memory for websites. They help the website remember things about you. Django, which is a tool used for building websites, uses sessions to keep track of your information, like if you’re signed in or what you like on the site. It gives each visitor a special ID, like a secret code, so when you come back, it knows it’s you. This special ID is stored in a cookie on your computer. The actual details, like your preferences, are kept safe on the website’s computer. It’s like having a secret key that only you and the website know, so your information stays secure.
Django sessions give developers a simple way to manage session data in views and templates, making it easy to share data between different parts of the application. To sum up, Django sessions offer a secure and customized online experience by streamlining the handling of user-specific data. They facilitate easy communication between the client and the server, giving developers the tools they need to easily design dynamic online applications.
For security purposes, instead of storing important data directly in the cookie on the user’s computer, Django keeps that data safe on the server side, kind of like in a secret vault (usually in a database). The only thing the cookie on your computer has is a special ID that helps the server recognize you. So, it’s like having a key to a secret locker – the key is in your pocket (the cookie), but the important stuff is kept safe in the locker (on the server). This way, even if someone looks at your key, they can’t access the valuable information without the server’s permission. Sessions also come in handy if someone’s web browser doesn’t like cookies, as they still work without any issues.
Steps to use session in Django
Firstly, we need to make sure that our Django project has session support enabled before we start using sessions. To enable sessions, there are two actions to follow.
1. Add ‘django.contrib.sessions‘ to INSTALLED_APPS:
# settings.py
INSTALLED_APPS = [
# …
‘django.contrib.sessions’,
# …
]
2. Add ‘django.contrib.sessions.middleware.SessionMiddleware’ to MIDDLEWARE:
# settings.py
MIDDLEWARE = [
# …
‘django.contrib.sessions.middleware.SessionMiddleware’,
# …
]
S
ession support in your Django project is enabled with these configuration.
Selecting and Recovering a Session Value
Picking and getting back something saved in a session in Django is quite easy. Let me explain with an example:
Setting a Session Value: In Django, you can store information in sessions using a dictionary-like object. It’s like having a special box to keep things. Imagine we want to save a greeting message, like ‘Hello, Django Sessions!’. Here’s how you do it in Django code:
from django.shortcuts import render def set_session(request): # Save a greeting message in the session request.session['greeting'] = 'Hello, Django Sessions!' return render(request, 'session.html')
Now, whenever someone visits, we put ‘Hello, Django Sessions!’ in the special box under the name ‘greeting’.
Getting a Session Value: To get that saved greeting back, we can use a template. Think of a template like a magic spell to show things. Here’s an example template:
<!-- session.html --> <!DOCTYPE html> <html> <head> <title>Session Example</title> </head> <body> <h1>Session Example</h1> <p>Session Value: {{ request.session.greeting }}</p> </body> </html>
In this template, {{ request.session.greeting }}
is like asking the magic box for the ‘greeting’. It shows the saved ‘Hello, Django Sessions!’ when the webpage is visited. So, setting and getting things from a session is like storing and retrieving treasures in a special box.
Example:
Django Session Methods