Sessions framework can be used to provide persistent behaviour for anonymous users in the website.Sessions are the mechanism used by Django for you store and retrieve data on a per-site-visitor basis.Django uses a cookie containing a special session id.
To enable the session in the django, you will need to make sure of two things in
- MIDDLEWARE_CLASSES has ‘django.contrib.sessions.middleware.SessionMiddleware’ activated
- INSTALLED_APPS has ‘django.contrib.sessions’ added.
After enabling the session, the session database table has to create and to do this run the following command:
python manage.py syncdb
After running previous command and if it didn’t find any errors then later run the command given below to finally reflect the changes saved onto the migration file onto the database.
python manage.py migrate
Now once sessions are created, then testing of the cookies has to be done. In
views.py, set the test cookie in the index view, and test the cookie in your about view.
To see the work done till now.
- First run the localhost through this command.
python manage.py runserver
- Then Open http://localhost:8000/ in the browser.
- Visit the index page then visit the about page. The “Cookie Tested!” will be printed out to the console.
To know how many times the site has been visited.You have to do following two things In views.py:
- Add and update the code in the index view function/li>
- Update the about view function
- JWT Authentication with Django REST Framework
- Django REST Framework Installation
- Serializers - Django REST Framework
- ModelSerializer in serializers - Django REST Framework
- HiddenField in serializers - Django REST Framework
- HyperlinkedModelSerializer in serializers - Django REST Framework
- DictField in serializers - Django REST Framework
- JSONField in serializers - Django REST Framework
- How to Create a basic API using Django Rest Framework ?
- Best practices for Professional Developer - Django Framework
- ListField in serializers - Django REST Framework
- Serializer Fields - Django REST Framework
- Voting System Project Using Django Framework
- URL fields in serializers - Django REST Framework
- IPAddressField in serializers - Django REST Framework
- Creating and Using Serializers - Django REST Framework
- Numeric fields in serializers - Django REST Framework
- Implement Token Authentication using Django REST Framework
- Boolean Fields in Serializers - Django REST Framework
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.