Check admin login on my django app

偶尔善良 提交于 2021-02-11 18:06:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!