Django : Use django session in HTML and views.py
I am using Django 3.1.2 (In other versions also, it should be the same thing)
First of all, some points should be clear :
While using django login/logout, a session method is used.
When an user logs into his account from a browser, a session is made for him. When that user logs out of his account, the session is deleted or expired. Session is usually saved in the browser cookies, that’s why you get your account logged in unless you log it out.
Each login is a session to be simple. A session is made for only one browser. That means if you switch browser, the session won’t be there because cookies won’t be shifted there. You will have to create another session by logging into your account.
If you clear browser cookies, your account will be logged out.
Important Concern
If you don’t use django built-in db.sqlite3 database, then to use session you may add these two lines in your settings.py file. Otherwise it will throw an error (Screenshot attached).
Using Session in views.py
In an app of django, views.py is a python file which is already created by django. If you use session in this python file, you don’t need to import anything. You can directly use it. To use it here (simple syntaxes):
Step 1 : Keep your username/email/password etc in your database.
Step 2 : While logging in, authenticate your username and password, I mean match input data with your database data.
Step 3 : If user is authenticated, then use this:
request.session[‘username’] = yourUsername
request.session[‘userLogged’] = True
Step 4 : request.session will take your current session. Btw, session is nothing but a dictionary (separate dictory for each request). if you use step 3 code, the current session dictionary will be updated with your current username and many others (I have only used this two dictionary, you can use many more).
Step 5 : You can access your username from anywhere of your project source code without passing any parameter. to call the current session userdata:
current_username = request.session[‘username’]
current_login_or_not = request.session[‘userLogged’]
you also can use this (but no need):
current_username = request.session.get(‘username’)
current_login_or_not = request.session.get(‘userLogged’)
To delete session (To log out, session must be deleted):
del request.session[‘userLogged’]
del request.session[‘username’]
Note: This calling is only for python files. I mean you can’t use this syntax to call usersession data outside python files. Sometimes you need to call usersession data from HTML files. I will show how to do this. Follow rest of this article.
Using session in HTML
If you want to call your session for your session data, you can’t call like python syntax. You will have to use little changed syntax here otherwise it will not work. You should see this error:
So, you can’t use the syntaxes written upwards to access session in HTML. In HTML if you want to know your username you must use:
request.session.username
If you want to know if you are logged in or not:
request.session.userLogged
To use django conditions:
{% if request.session.userLogged %}{% else %}{% endif %}
To print your username in HTML page:
<p>Your username is {{request.session.username}}</p>
To know more about it, visit the django documentation. Here is the link.