What is Django Session | Django Tutorial for Beginners
August 1, 2021 2022-12-18 1:50What is Django Session | Django Tutorial for Beginners
What is Django Session | Django Tutorial for Beginners
What is Django Session
In this article we will discuss Django Session, if you are learning web development then you must need to clarify what is a session in website development is, why its need and its importance. We will talk about Django Cookies, Django Cache (all types) in separate articles for beginner students.
As you know that we are learning Django, surely you will learn about Session in this article. But for your knowledge, If you have understood what is cookie in detail and with a clear concept then there are no difficulties for you to learn Session. Because there is some difference between session and cookie.
What is a Session?
The session is the period of time duration which spend a user on any website, if you are developing a registration or login system then we use session techniques, using session we can store user information, its ID, email, username, etc. On the basis of its session, we can check wheater the user is logger or logout. When a session starts, it must destroy because you know in the real world, when any educational session is started, after 2 or 4 years their session is closed. Same like that, there is a concept.
What is Cookies?
A cookie is the client-side When a user visits any website, that website server sends some information to the user’s browser, you can find it in browser settings. We can get useful information using its cookies-based information. Sometimes we used it on the eCommerce website
Django Session vs Cookie
What types of Django sessions to store data
- There are different ways to use Session for your Django project. You can overview to use them.
- sessions in views
- cookie-based sessions
- file-based sessions
- cached sessions
Why need to use Django Session?
- Actually, we want to store user information when visiting our website. We want to track users and other relevant information. Sometimes a session is used to check wheater the user is login to its account or not. Because when a user login to its account, its session start, during its session and login period, we can track user its all information but when its logout we cannot do that.
- Some time user did not allow to store cookies in its browser, then we can use the session in this case.
- A cookie is less secure but session is secure than it, that is why we use it.
How to install Django Session?
To use the session on your Django website, you have to do some settings. If you did not do this, your session will not work unstill you install it. But it is done by default, anyhow if you find it as a miss, you can do this.
So following two references of the session to use, one for middleware and the other for an installed app.
You have to open settings.py file and incldue in the middle ware setting:
MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
…..
]
After doing the middleware setting, go to the installed app section, and include the following app for the session.
INSTALLED_APPS =
[ …
‘django.contrib.sessions ‘, …
]
After adding the above lines of code, you have to run the following command to get a database table to store session information to a user.
python manage.py makemigrations
Then:
python manage.py migrate
It will create an extra table in your default database or another if you are using a related session, in which user session-related information will be stored there but its id will in the user browser.
How to work with Django Session Handling?
Then open the views.py file and work with session commands:
1. When you run the following line of code it will store a value in the session of ‘key’. In the place key, you can add another kye also.
request.session[‘key’] =”value”
request.session[‘name’] =”Faisal Zamir”
So, here the key is the ‘name’ and the value is the ‘Faisal Zamir’.
2. When we print request.session[‘name’] we will get a value store in it as ‘Faisal Zamir’
3. The value we got using session we can store that value in a variable when required we can use that.
session_value = request.session[‘key’]
session_value = request.session[‘name’]
session_value will contain the value of ‘name’ that is ‘Faisal Zamir’
4. Sometimes, we need to use default arguments, which help, when sessions have no value for a specific key. Then it will return a default value that we have set.
session_value = request.session.get(‘key’,’default’)
session_value = request.session.get(‘key’,’Zamir Hussain’)
5. If I print the value of print session_value, it will return ‘Faisal Zamir’, if we have not set its value then it will return its default value that is ‘Zamir Hussain’
6. If you want to delete a session key with its value, you can use the following method.
del request.session[‘key’]
7. If we have a session with key ‘name’ we can use the following line of code to delete the session with a specific key
del request.session[‘name’]
8. In case that, if you have mentioned a key that does not exist then it will raise a key error.
9. request.session.keys(): It will return all the keys which have been created to store values
10. request.session.items(): It will return all the values you have assign to keys
Now! what you have to do?
As you have understood this article about “Django Session”, now you must follow instructions as discussed in this article. In case of any issues or problems, don’t worry, you can discuss them with us.
On other hand, If you have any suggestions or knowledge about this article, you can share it with us, we will appreciate you!
If this article is good then share it on Facebook, Twitter, Pinterest, Instagram, etc.
Advanced Tutorials for Beginners
Can Web Design using Python Programming?
Complete Built-in Django Form Fields with Examples
Complete Guide on Django ORM | Django Tutorials