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
- Implement Token Authentication using Django REST Framework
- Introduction to Kivy ; A Cross-platform Python Framework
- Weather app using Django | Python
- Django URL patterns | Python
- Views In Django | Python
- Python | Django News App
- Django Migrations | Python
- Complete Django History | Python
- Python | Django Admin Interface
- Python | Form validation using django
- Intermediate fields in Django | Python
- Python | ToDo webapp using Django
- Python | Uploading images in Django
- Django manage.py migrate command | Python
- Python | Django-allauth setup and Configuration
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.