Ad

Django: Can I Prevent Logged In Users From Accessing Login.html?

I have two general questions:

  1. As per the title, is there a way to make users who have an active session running that try to access /login.html be routed to /home.html instead?

  2. I'm using Django's default PasswordChangeForm and when a user changes their password, it lets them use their old password. So this works:

Old Password: 123456

New Password: 123456

Repeat New Password: 123456

Thanks!

Ad

Answer

For your first question, assuming that you serve the /login.html through a View, you can easily check if the user is logged in and in that case serve a 302 redirect like this.

from django.shortcuts import redirect

def my_view(request):
    if request.user.is_authenticated:
        return redirect('/home.html')

Full documentation https://docs.djangoproject.com/en/1.11/topics/auth/default/#the-raw-way

Ad
source: stackoverflow.com
Ad