Django : Use django session in HTML and

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 file. Otherwise it will throw an error (Screenshot attached).

Using Session in

In an app of django, 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:

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:

you also can use this (but no need):

To delete session (To log out, session must be deleted):

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:

If you want to know if you are logged in or not:

To use django conditions:

To print your username in HTML page:

To know more about it, visit the django documentation. Here is the link.

CS Undergrad | BUET

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store