Search code examples
pythondjangodjango-viewsdjango-sessions

Django Session Management


I am creating a Django based web application. I want to learn how to manage session in that. I browsed several tutorials but not got appropriate reference. I have 3 urls in my urls.py with 3 seperate function to handle it. 1. Login (login.html with login function) 2. Homepage (homepage.html with homepage function) 3. Logout (logout.html).

I able to create session on Login page successfully once credential has been verified but I am not able to redirect it perfectly on Homepage and homepage function. So Logout button, on homepage not able to work perfectly.

So in sort I want to know how to redirect pages with functional calls, not only HTML page. So homepage function also can work.


Solution

  • ...but I am not able to redirect it perfectly on Homepage and homepage function

    Your problem is not about sessions. Basically you need to,

    • Redirect users to home page after successful login attempt
    • Redirect users to login page after logout attempt

    I would advise you to use built-in authentication views:

    Django provides several views that you can use for handling login, logout, and password management. These make use of the stock auth forms but you can pass in your own forms as well.

    With these built-in authentication views you can also set default redirection url after a successful login attempt in your settings.py file:

    LOGIN_REDIRECT_URL = '/home/'
    

    You can find more details about AUTH SETTINGS here

    If you don't want to use Django's auth system and use your own views, You can find more details and some examples here.

    And yes, Django has one of the most detailed and clean documentation web site on the internet. It is easy and fun to view!