Login URL redirecting issue

When I log into my web application, it doesn’t redirect to the custom redirect page created instead it redirects to the default accounts/profile url in Django. Below are my codes :slight_smile:

views.py

def student_dashboard(request):
    if request.user.is_authenticated and request.user.is_student:
        render(request,'student/s_dashboard.html')
    elif request.user.is_authenticated and request.user.is_client:
        return redirect('client_dashboard')
    elif request.user.is_authenticated and request.user.is_supervisor:
        return redirect('supervisor_dashboard')
    else:
        return redirect('signin')
 def supervisor_dashboard(request):
    if request.user.is_authenticated and request.user.is_student:
        return render(request,'student/s_dashboard.html')
    elif request.user.is_authenticated and request.user.is_client:
        return redirect('client_dashboard')
    elif request.user.is_authenticated and request.user.is_supervisor:
        return redirect('supervisor_dashboard')
    else:
        return redirect('signin')
    
 def client_dashboard(request):
     if request.user.is_authenticated and request.user.is_student:
         return render(request,'student/s_dashboard.html')
     elif request.user.is_authenticated and request.user.is_client:
         return redirect('client_dashboard')
     elif request.user.is_authenticated and request.user.is_supervisor:
         return redirect('supervisor_dashboard')
     else:
         return redirect('signin')

   def signin(request):
      if request.user.is_authenticated:
           if request.user.is_student:
               return redirect('student_dashboard')
           if request.user.is_cleint:
               return redirect('client_dashboard')
           if request.user.is_supervisor:
               return redirect('supervisor_dashboard')
 
      if request.method == 'POST':
           username = request.POST['username']
           password = request.POST['password']
           user = authenticate(request, username =username, password = password)
           print(user)
           if user is not None:
              login(request,user)
              if user.is_authenticated and user.is_student:               
                   return redirect('student_dashboard') #Go to student home
              elif user.is_authenticated and user.is_client:                                                                                    
                   return redirect('client_dashboard') #Go to teacher home      
              elif user.is_authenticated and user.is_supervisor:
                   return redirect('supervisor_dashboard')

     
            else:
                 form = AuthenticationForm()
                 return render(request,'registration/login.html',{'form':form})
 
       else:
            form = AuthenticationForm()
            return render(request, 'registration/login.html', {'form':form})

urls.py

from django.contrib import admin
from django.urls import path, include
from CPRS_admin.views import *
from CPRS_admin.HOD_views import * 

urlpatterns = [
    path("admin/", admin.site.urls),
    path("home/", home_view, name="home"),
    path("student/", student_view, name="student"),
    path("group/", group_view, name="group"),
    path("admin/", admin.site.urls),
    path("accounts/", include("django.contrib.auth.urls")),
    path("accounts/login/",signin,name="login"),
    path(
        "accounts/signup/student/", StudentSignUpView.as_view(), name="student_signup"
    ),
    path("accounts/signup/client/", ClientSignUpView.as_view(), name="client_signup"),
    path(
        "accounts/signup/supervisor/",
        SupervisorSignUpView.as_view(),
        name="supervisor_signup",
    ),
    path("coordinator/dashboard", admin_dashboard, name="admin_dashboard"),
    path("coordinator/search", search, name="search_page"),
    path("coordinator/projects", project, name="projects"),
    path("student/dashboard", student_dashboard, name="student_dashboard"),
    path("supervisor/dashboard", supervisor_dashboard, name="supervisor_dashboard"),
    path("client/dashboard", client_dashboard, name="client_dashboard"),
    path("main/",main_view,name="main"),
    path("client/addproject",AddProjectView.as_view(),name="add_project"),
    
    path("coordinator/student_list",student_view_list,name="student_list"),

    path("coordinator/project_list",project,name="project_list"), 

    path("coordinator/add_student_group",add_student_group,name="add_student_group"),
    path("coordinator/client_list",clientview_list,name="client_list"),
    
]

When I log into my web application, it doesn’t redirect to the custom redirect page created instead it redirects to the default accounts/profile url in Django. Below are my codes :

Views.py

def student_dashboard(request):
    if request.user.is_authenticated and request.user.is_student:
        render(request,'student/s_dashboard.html')
    elif request.user.is_authenticated and request.user.is_client:
        return redirect('client_dashboard')
    elif request.user.is_authenticated and request.user.is_supervisor:
        return redirect('supervisor_dashboard')
    else:
        return redirect('signin')
 def supervisor_dashboard(request):
    if request.user.is_authenticated and request.user.is_student:
        return render(request,'student/s_dashboard.html')
    elif request.user.is_authenticated and request.user.is_client:
        return redirect('client_dashboard')
    elif request.user.is_authenticated and request.user.is_supervisor:
        return redirect('supervisor_dashboard')
    else:
        return redirect('signin')
    
 def client_dashboard(request):
     if request.user.is_authenticated and request.user.is_student:
         return render(request,'student/s_dashboard.html')
     elif request.user.is_authenticated and request.user.is_client:
         return redirect('client_dashboard')
     elif request.user.is_authenticated and request.user.is_supervisor:
         return redirect('supervisor_dashboard')
     else:
         return redirect('signin')

   def signin(request):
      if request.user.is_authenticated:
           if request.user.is_student:
               return redirect('student_dashboard')
           if request.user.is_cleint:
               return redirect('client_dashboard')
           if request.user.is_supervisor:
               return redirect('supervisor_dashboard')
 
      if request.method == 'POST':
           username = request.POST['username']
           password = request.POST['password']
           user = authenticate(request, username =username, password = password)
           print(user)
           if user is not None:
              login(request,user)
              if user.is_authenticated and user.is_student:               
                   return redirect('student_dashboard') #Go to student home
              elif user.is_authenticated and user.is_client:                                                                                    
                   return redirect('client_dashboard') #Go to teacher home      
              elif user.is_authenticated and user.is_supervisor:
                   return redirect('supervisor_dashboard')

     
            else:
                 form = AuthenticationForm()
                 return render(request,'registration/login.html',{'form':form})
 
       else:
            form = AuthenticationForm()
            return render(request, 'registration/login.html', {'form':form})

urls.py

from django.contrib import admin
from django.urls import path, include
from CPRS_admin.views import *
from CPRS_admin.HOD_views import * 

urlpatterns = [
    path("admin/", admin.site.urls),
    path("home/", home_view, name="home"),
    path("student/", student_view, name="student"),
    path("group/", group_view, name="group"),
    path("admin/", admin.site.urls),
    path("accounts/", include("django.contrib.auth.urls")),
    path("accounts/login/",signin,name="login"),
    path(
        "accounts/signup/student/", StudentSignUpView.as_view(), name="student_signup"
    ),
    path("accounts/signup/client/", ClientSignUpView.as_view(), name="client_signup"),
    path(
        "accounts/signup/supervisor/",
        SupervisorSignUpView.as_view(),
        name="supervisor_signup",
    ),
    path("coordinator/dashboard", admin_dashboard, name="admin_dashboard"),
    path("coordinator/search", search, name="search_page"),
    path("coordinator/projects", project, name="projects"),
    path("student/dashboard", student_dashboard, name="student_dashboard"),
    path("supervisor/dashboard", supervisor_dashboard, name="supervisor_dashboard"),
    path("client/dashboard", client_dashboard, name="client_dashboard"),
    path("main/",main_view,name="main"),
    path("client/addproject",AddProjectView.as_view(),name="add_project"),
    
    path("coordinator/student_list",student_view_list,name="student_list"),

    path("coordinator/project_list",project,name="project_list"), 

    path("coordinator/add_student_group",add_student_group,name="add_student_group"),
    path("coordinator/client_list",clientview_list,name="client_list"),
    
]

This is the error that is shown after logging in. It redirects to the default url which is accounts/profile even though a custom redirect is added as seen in views.py.

Is it possible you need to modify LOGIN_REDIRECT_URL = in your settings.py file to change the default login redirect?

https://docs.djangoproject.com/en/4.0/ref/settings/#std:setting-LOGIN_REDIRECT_URL

What does your User model look like? Have you confirmed that one of those ‘is_’ conditions are true?

You have your custom urls after the system urls. The system does the dispatch based on the first match. You views won’t be called for any view with the same url as a system-provided url.

Also, double-check the indentation in your code. What you posted makes it look like your signin method is inside client_dashboard.

hiii, you have not created HTMl template, which you have mentioned in URLS.py,Maybe., i am having login issues in my website, let me know , are you able to do login in your website as a normal user ?