How do i redirect back to the login page after 10 seconds of inactivity using django?

Viewed 799

In my settings.py I state this and the sessionId did expire but it redirect me to some random links that I never state at all instead back to the login page and will show some error, I want it to redirect back to the login page after 10 seconds of inactivity, how do I do that?

My settings.py

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django_session_timeout.middleware.SessionTimeoutMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',


]

    SESSION_EXPIRE_SECONDS = 10  
    
    SESSION_EXPIRE_AFTER_LAST_ACTIVITY = True
    
    SESSION_EXPIRE_AT_BROWSER_CLOSE = True

My views.py

 def login_view(request):
        form = LoginForm(request.POST or None)
        msg = None
        if request.method == 'POST':
            if form.is_valid():
                username = form.cleaned_data.get('username')  # retrieve the username
                password = form.cleaned_data.get('password')  # retrieve the password
                user = authenticate(username=username, password=password)  # authenticate user
    
                if user is not None and user.is_admin:  # authenticated if user is an admin
                    login(request, user)
    
    
                    return redirect('adminpage')  # redirect the user to the admin page
    
                elif user is not None and user.is_customer:  # authenticated if user is a customer service
                    login(request, user)
                    return redirect('customer')  # redirect the user to the customer service page
    
                elif user is not None and user.is_logistic:  # # authenticated if user is a logistic
                    login(request, user)
                    return redirect('logistic')  # redirect the user to the logistic page
                else:
                    msg = 'invalid credentials'
    
            else:
                msg = 'error validating form'
        return render(request, 'login.html', {'form': form, 'msg': msg})
def admin(request):

    return render(request, 'admin.html')

My urls.py

urlpatterns = [
    path('', views.index, name='index'),
    path('login/', views.login_view, name='login_view'),
    path('register/', views.register, name='register'),
    path('adminpage/', views.admin, name='adminpage'),
    path('customer/', views.customer, name='customer'),
    path('logistic/', views.logistic, name='logistic'),
    path('forget/', views.forget, name='forget'),
    path('newblock/', views.newblock, name='newblock'),
    path('quote/', views.quote, name='quote'),
    path('profile/', views.profile, name='profile'),
    path('adminprofile/', views.adminprofile, name='adminprofile'),
]

This is the error message it show me after the session expire:

enter image description here

2 Answers

The problem is about your login url :

Django try to redirect on http://localhost:8000/accounts/login/ but this url is not defined in any urls.py file.

Add LOGOUT_REDIRECT_URL = reverse_lazy('login_view') in the settings.py file. (don't forgot from django.urls import reverse_lazy).

'django_session_timeout.middleware.SessionTimeoutMiddleware' calls django.contrib.auth.views.redirect_to_login if you are not provided SESSION_TIMEOUT_REDIRECT value in your settings. Details

redirect_to_login internally uses settings.LOGIN_URL value https://docs.djangoproject.com/en/3.2/ref/settings/#login-url

Looks like you have two options: set SESSION_TIMEOUT_REDIRECT or change LOGIN_URL value in your settings. I'd recommend to change the second one.

Related