Allowing only the specific users or group of users to visit the specific page of the website in Django?

Viewed 448

Let us suppose that there is a admin page in my website designed by me for doing some actions on the website. now i want that only few user that belongs to a specific can visit that page. other users should redirected or should have see an error page when they try to go at that page.

for karan(admin ) can visit that page but not ram(simple user)

you can also refer any link from the django docs

thanks!

1 Answers

You can use the permission required decorator for function based views or the permission required mixin for class based views.

https://docs.djangoproject.com/en/3.2/topics/auth/default/#the-permission-required-decorator

To create a custom permission you can use the PermissionsMixin on your models.

class CustomUser(AbstractBaseUser, PermissionsMixin):
    class Meta:
        permissions = (
                ("validator","Can access validation views"),
                ("administrator","Almost all permissions, but not a superuser"),
                ("teacher","user to create question"),
        )
Related