Skip to content

Instantly share code, notes, and snippets.

@gtindo
Last active April 8, 2019 14:12
Show Gist options
  • Save gtindo/65ce6c0879d1e58f2106bb495b5e3856 to your computer and use it in GitHub Desktop.
Save gtindo/65ce6c0879d1e58f2106bb495b5e3856 to your computer and use it in GitHub Desktop.
A Django mixin for checking user status and redirect
from django.contrib.auth.mixins import AccessMixin
from django.shortcuts import redirect
class UserIsStaffRequired(AccessMixin):
"""Verify that the current user is staff member."""
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated :
return self.handle_no_permission()
else:
if not request.user.is_staff :
return redirect("/")
return super().dispatch(request, *args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment