Django Group permission how to print it in template

前端 未结 2 769
故里飘歌
故里飘歌 2021-02-09 06:56

if the customer sign in and he has permission to see the data that the admin has given to him, he will see the data after he sign in, but if the admin doesn\'t give him permissi

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-09 07:34

    def staff_only(view_func):
        def wrapper_function(request, *args, **kwargs):
            group = None
            if request.user.groups.exists():
                group = request.user.groups.all()[0].name
            if group == 'registrar':
                return redirect('adminpage')
    
            if group == 'admin':
                return view_func(request, *args, **kwargs)
            return redirect("loginpage")
    
        return wrapper_function
    

    This function is missing case when a user does not have required groups, in this case, either we should either redirect a user to login page send to another page or display like "You don't have access to this page"

提交回复
热议问题