Design a site like this with WordPress.com
Get started

View Session information

Lets see how to view session information in django

From our previous tutorial, we now know how to setup authentication. We know how to create an admin user or a normal user. here is the git link if you have not gone through the tutorial already. https://letsblogcontent.wordpress.com/2020/09/18/django-authentication/

We are going to build on from the previous tutorial – https://letsblogcontent.wordpress.com/2020/09/18/django-authentication/

You can start directly from this tutorial, just copy the contents from the git repo “pip install -r requirements.txt”

Step 1 – We will now create an app to view session information. lets call this app “admin-data-viewer”. Execute below command

python manage.py startapp AdminModels

Register the app into the project under the INSTALLED_APPS object

Step 2 – This will create a new app with admin.py inside the app.

from django.contrib import admin
from django.contrib.sessions.models import Session


class SessionAdmin(admin.ModelAdmin):
    def _session_data(self, obj):
        return obj.get_decoded()
    list_display = ['session_key', '_session_data', 'expire_date']

admin.site.register(Session,SessionAdmin)

Step 3 – If you have not created a user, lets create a super user. Execute below command and follow instructions

python manage.py createsuperuser

Step 4 – execute makemigrations and migrate

python manage.py makemigrations
python manage.py migrate

Step 5 – Run the server

python manage.py runserver

Step 6 – Navigate to http://127.0.0.1:8000/admin” and provide the admin username and password created in step 3 and look for b

Click on sessions to see below information about session

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: