0

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'
Khubaib Khawar
  • 51
  • 1
  • 11
  • Redirect to proper template programmatically depending on the profile (student or teacher). – oz19 Feb 14 '20 at 09:00
  • I would suggest you to extend the auth_user table and add type of user there, so that you can have a common interface and you can redirect user to the view they deserve based on the type of user. – BATMAN Feb 14 '20 at 09:03
  • how do you differentiate between a student and a teacher? is there any field in db? or is it just different links for login for each of them? – Nalin Dobhal Feb 14 '20 at 09:41
  • @BATMAN I am new to Django and I don't know yet how to extend the auth_user table, there was an answer posted by someone, I thought that would work, but that answer is deleted now. – Khubaib Khawar Feb 14 '20 at 09:48
  • @NalinDobhal if I am not wrong, I guess you answered the question earlier and I was going to apply that answer but it is deleted now, if you were the one, can you post that answer again? By the way yes, Student and Teacher are two Tables in DB. – Khubaib Khawar Feb 14 '20 at 09:49
  • it wasn't me who answered your question. But you haven't answered the question that how are you identifying if student is trying to log in or teacher? – Nalin Dobhal Feb 14 '20 at 09:52
  • @NalinDobhal basically, student and teacher are two different external user to the system. The db contains the default user model for the details of both student and teacher. I have total 3 apps in Django, one for main system, second for student and third for teacher. student and teacher both have their separate login pages, register pages and log out pages, now, I have added `LOGIN_REDIRECT_URL` in `settings.py` for student, and I want something similar for teacher as well, so that teacher can redirect to the teacher's login page and not the student's login page. – Khubaib Khawar Feb 14 '20 at 09:55
  • @NalinDobhal I have two buttons in home page of the system one states "I am a Student" and other states "I am a Teacher", now, I have set perfectly for student, when someone clicks on I am a Student he will be redirected to student's login page, I know that I can do it for Teacher easily by putting tag but issue is that, I want it to redirect to teacher's login page when someone tries to access `localhost:8000/teacher.html`. and for that I need something like `LOGIN_REDIRECT_URL` in settings.py as I have for student. – Khubaib Khawar Feb 14 '20 at 09:58
  • Someone posted the answer to add `LOGIN_REDIRECT_URL` more than once in settings.py but he/she stated the code that we had to enter in `apps.py` in the specific app (in my case teacher's app), and if I add that code in apps.py then I would be able to write `LOGIN_REDIRECT_URL_FOR_TEACHER = teacher-login`. But that answer is removed and I don't know where to find that code that I have to add in `apps.py` – Khubaib Khawar Feb 14 '20 at 10:01
  • @NalinDobhal correction in my comment just after yours'. I want something similar for the teacher as well, so that teacher can redirect to `teacher.html` page after logging in successfully. addition to my second last comment: and I want teacher to be redirected to `teacher.html` using `LOGIN_REDIRECT_URL`. – Khubaib Khawar Feb 14 '20 at 10:08
  • I need that answer :( I was working on my files and I thought to apply that later but now the answer is removed :( – Khubaib Khawar Feb 14 '20 at 10:10
  • You can't add a second `LOGIN_REDIRECT_URL` to settings, but you can either override the login view or create an intermediate view that redirects to the correct page. See the answer to [this question](https://stackoverflow.com/questions/36092760/django-login-and-redirect-to-user-profile-page). – Alasdair Feb 14 '20 at 10:25
  • I think you are confused about `LOGIN_URL` and `LOGIN_REDIRECT_URL`. `LOGIN_REDIRECT_URL` is when the user is sent to *after* they have logged in. Usually, you would have the same login page (`LOGIN_URL`) for students and teachers. – Alasdair Feb 14 '20 at 10:28
  • @Alasdair I am not confused. I actually want two login pages, one for teacher that says 'Login as a teacher' and other for students that says 'Login as a student'. I am sure about how to control `LOGIN_URL`, what I don't know is that how to redirect teacher to `teacher.html` from the Login Page. and for that I need something similar to `LOGIN_REDIRECT_URL` in `settings.py`. Someone answered about how to do this but the answer is removed now. – Khubaib Khawar Feb 14 '20 at 10:34
  • If you have two different login URLs, then show the URL patterns (and views if you have one) in your question. – Alasdair Feb 14 '20 at 11:16
  • already shown @Alasdair, please have a look – Khubaib Khawar Feb 14 '20 at 11:18
  • No, I can't see your login view or URL pattern in your question. – Alasdair Feb 14 '20 at 11:18
  • BTW the deleted answer that was removed because the approach used in it won't work. – Alasdair Feb 14 '20 at 11:19
  • @Alasdair I don't think that has anything to do with it. It's just simple that I have two separate apps names student and teacher, now user (student) is redirected to `student.html` using `LOGIN_REDIRECT_URL = 'student'` in settings.py, now how do I redirect user (teacher) to `teacher.html` from the `login.html` that is in teacher app? – Khubaib Khawar Feb 14 '20 at 11:26
  • *I don't think that has anything to do with it*. Fair enough, but if you don't show the code people ask for, then they have to guess so you might not get as many replies. – Alasdair Feb 14 '20 at 12:43
  • @Alasdair I added the code from `urls.py` of the main project, can you check now and see if this is the code you wanted to see? – Khubaib Khawar Feb 14 '20 at 12:57
  • Please help me, I need to submit this project with in 4 hours. – Khubaib Khawar Feb 14 '20 at 12:58

1 Answers1

2

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'),
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thanks @Alasdair, this looks a promising answer. Can you tell me how to use `reverse()` in the `return` statement? Any example using my scenario? – Khubaib Khawar Feb 14 '20 at 19:52
  • It depends on what the name of the URL pattern you are redirecting to is. See the docs for [`reverse`](https://docs.djangoproject.com/en/3.0/ref/urlresolvers/#reverse). It's just a recommendation to use `reverse`, the example code will work if you hardcode the URL. – Alasdair Feb 14 '20 at 20:23
  • Thank you so much @Alasdair – Khubaib Khawar Feb 14 '20 at 21:18