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')
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):
...
thank you for this, i had issue with caching... I just needed to restart server
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.