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

Sey*_*aid 5 python django django-templates

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

  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
 ]
Run Code Online (Sandbox Code Playgroud)

在我的views.py我有以下功能进行身份验证

    def  my_custom_function(request):
         if request.user.is_superuser():
            #... redirect to django admin login page

         else:
             # return render(404_page)
Run Code Online (Sandbox Code Playgroud)

是这样的。

rud*_*dra 2

默认情况下,django admin 仅允许超级用户或 stuff 用户登录。因此,拥有管理员登录面板是安全的。另外,如果您想限制该登录路径,我认为最好在该特定路由上放置防火墙。这样只有列入白名单的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;
}
Run Code Online (Sandbox Code Playgroud)

这篇文章可能对配置有所帮助。