简体   繁体   中英

Is there any way to make a second LOGIN_REDIRECT_URL in settings.py in Django?

I am creating an app in Django which have two external users: 1. Teacher 2. Student

I have to create total of 3 apps, one the base app that contains home.html template, other the student app that contains student.html template and third teacher app that contains teacher.html template.

I have just created two apps for now, base app and student app, I have created login, logout and register pages for student app, now I am successfully able to redirect the user (student) to the student.html whenever the student logs into the system and I did this by putting LOGIN_REDIRECT_URL = 'student' in my settings.py .

I want to do the same for Teacher app as well but I want to redirect teacher to the teacher.html .

Is there any way that I can create the second LOGIN_REDIRECT_URL in settings.py to fulfil this purpose or it will be done any other way?

My Project Structure

django_project

|__esacp(main system)

|_____migrations

|_____templates

|________esacp

|__________base.html

|__________home.html

|__________student.html

|__________teacher.html

|_____apps.py

|_____forms.py

|_____models.py

|_____views.py

|__django_project

|__student

|_____migrations

|_____templates

|________student

|___________login.html

|___________logout.html

|___________register.html

|_____apps.py

|_____forms.py

|_____models.py

|_____views.py

|__teacher

|__db.sqlite3

|__manage.py

Code of models.py of esacp app

from django.db import models
from django.utils import timezone

class StudentType(models.Model):
    studenttype_id = models.IntegerField(primary_key=True)
    type_title = models.CharField(max_length=50)

    def __str__(self):
        return self.type_title

class StudentDetails(models.Model):
    name = models.CharField(max_length=50)
    username = models.CharField(max_length=50, primary_key=True)
    password = models.CharField(max_length=50)
    email_id = models.CharField(max_length=100)
    contact = models.CharField(max_length=100)
    studenttype = models.ForeignKey(StudentType, on_delete=models.CASCADE)
    registration_date = models.DateTimeField(default=timezone.now)
    modify_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

Code of urls.py of esacp app

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='esacp-home'),
    path('student', views.student, name='student'),
]

Code of urls.py of main project

from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from users import views as user_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/', user_views.register, name='register'),
    path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
    path('tlogin/', auth_views.LoginView.as_view(template_name='teacher/tlogin.html'), name='tlogin'),
    path('tlogout/', auth_views.LogoutView.as_view(template_name='teacher/tlogout.html'), name='tlogout'),
    path('esacp/', include('esacp.urls')),

Code of views.py of teacher app

from django.shortcuts import render
from django.contrib.auth.views import LoginView

class MyLoginView():

    def get_success_url(self):
        url = self.get_redirect_url()
        return url

Code of views.py of esacp app

from django.shortcuts import render
from django.contrib.auth.decorators import login_required

def home(request):
    return render(request, 'esacp/home.html')

@login_required
def student(request):
    return render(request, 'esacp/student.html')

and below I have the statement in setting.py

LOGIN_REDIRECT_URL = 'student'

You have two login URLs. Therefore you can use a different login view for each one, and override get_success_url so that you are redirected to the correct page after login.

For example, the teacher login view would look something like:

from django.contrib.auth.views import LoginView

class TeacherLoginView(LoginView):
    template_name = 'teacher/tlogin.html'

    def get_success_url(self):
        url = self.get_redirect_url()
        return url or '/teacher/' # FIXME use reverse here instead of hardcoding the URL 

Then use that view instead of TeacherView in your URL patterns.

path('login/', TeacherLoginView.as_view(), name='login'),

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