问题
I have created a django app and have linked to the admin index page by modifying base.html. However, the link of the app is accessible directly as well.
Can I check on the page or in the views.py of my app if the user is logged in to the django admin or not?
回答1:
Use the @staff_member_required
decorator:
from django.contrib.admin.views.decorators import staff_member_required
@staff_member_required
def my_view(request):
...
回答2:
from django.contrib.auth.decorators import user_passes_test
@user_passes_test(lambda u:u.is_staff, login_url='/foo/')
def staff_view(request..):
...
来源:https://stackoverflow.com/questions/29362857/check-admin-login-on-my-django-app