gpt4 book ai didi

python - Django admin.site.urls only access to superuser (admin login page only access to superuser)

转载 作者:行者123 更新时间:2023-12-02 02:47:08 26 4
gpt4 key购买 nike

我想要的是将 django 管理员登录页面的访问权限限制为只有 super 用户。这意味着如果您不是 super 用户,并尝试访问 http://127.0.0.1:8000/admin - 你应该被重定向到 404 页面,类似的东西。执行此身份验证的方法或自定义 View 是挑战。请有人帮助我提示如何做?

  urlpatterns = [
path('admin/', my_custom_function,name="check_if_superuser"),


# when somebody hits this url pattern , he/she should be taken to the
# function above for checking if superuser befor being redirected to
# django admin login page
]

在我的 views.py我有以下功能可以进行身份​​验证
    def  my_custom_function(request):
if request.user.is_superuser():
#... redirect to django admin login page

else:
# return render(404_page)

是这样的。

最佳答案

默认情况下,django admin 只允许 super 用户或东西用户登录。因此,拥有管理员登录面板是安全的。另外,如果您想限制该登录路径,我认为最好在该特定路由上放置防火墙。这样只有列入白名单的 IP 才能访问它。您可以为此使用 NGINX,配置应该是这样的:

location /admin {
# block one workstation
deny 192.168.1.1;
# allow anyone in 192.168.1.0/24
allow 192.168.1.0/24;
# drop rest of the world
deny all;
}

article可能对配置有所帮助。

关于python - Django admin.site.urls only access to superuser (admin login page only access to superuser),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54087979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com