简体   繁体   中英

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:

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.

错误

You are never going to your own signin view as the path before also includes a.../login/ via the included auth.urls:

    path("accounts/", include("django.contrib.auth.urls")),
    path("accounts/login/",signin,name="login"),

You can change the order:

   path("accounts/login/",signin,name="login"),
   path("accounts/", include("django.contrib.auth.urls"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM