'How to prevent different user types to view each other dashboard in django

I have a website with 3 user types, admin, instructor and students, I used login mixing to redirect each of them to their respective dashboard but if the student modify their url to /instructor or /admin ; it will give them access to the page; I want to restrict student to only student page and admin to only admin page.

I tried writing the function

If user.student.is_authenticated: class Student_dashboard(): ......

However it's not restricting them



Solution 1:[1]

You can use the UserPassesTestMixin mixin to add a test function to validate that users have permission to access the view

from django.contrib.auth.mixins import UserPassesTestMixin
from django.views import View


class Student_dashboard(UserPassesTestMixin, View):

    def test_func(self):
        return self.request.user.is_student

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iain Shelvington