Home > Enterprise >  allow page access only for admin user Django
allow page access only for admin user Django

Time:10-05

So I need to allow page access only for admin user in Django, I'm trying this piece of code to use in app views.py, but its not working well..

from django.shortcuts import redirect

def index(request):
    if not request.user.is_superuser:
        return redirect('https://xx.com')
    return render(request, 'scan_debug/index.html')

CodePudding user response:

You can use user_passes_test decorator:

Then you can create an admin_check method like so:

def admin_check(user):
   return user.is_superuser

Pass the admin_check method to the decorator like so:

@user_passes_test(admin_check)
def my_view(request):
    ...

CodePudding user response:

thank you for this, i had issue with caching... I just needed to restart server

  • Related