Python 如何在基于django类的视图中限制对某些组的访问
MyPython 如何在基于django类的视图中限制对某些组的访问,python,django,django-views,Python,Django,Django Views,Myviews.py混合了def和ClassViews: @login_required(login_url='login') @allowed_users(allowed_roles=['Admin', 'Staff', 'Lite Scan']) def litescan(request): filteredOutput = Stock.objects.all() val = {}... @method_decorator(login_required(login_url=
views.py
混合了def
和ClassViews
:
@login_required(login_url='login')
@allowed_users(allowed_roles=['Admin', 'Staff', 'Lite Scan'])
def litescan(request):
filteredOutput = Stock.objects.all()
val = {}...
@method_decorator(login_required(login_url='login'), name='dispatch')
class HomeView(ListView):
model = Post
template_name = 'community.html'
ordering = ['-id']
下面是我的decorators.py
,如果这有帮助的话:
from django.shortcuts import redirect
from django.http import HttpResponseRedirect
def unauthenticated_user(view_func):
def wrapper_func(request, *args, **kwargs):
if request.user.is_authenticated:
return redirect('home')
else:
return view_func(request, *args, **kwargs)
return wrapper_func
def allowed_users(allowed_roles=[]):
def decorator(view_func):
def wrapper_func(request, *args, **kwargs):
group = None
if request.user.groups.exists():
group = request.user.groups.all()[0].name
if group in allowed_roles:
return view_func(request, *args, **kwargs)
else:
url = ('/forbidden')
return HttpResponseRedirect(url)
return wrapper_func
return decorator
我发现@login\u required
和@allowed\u用户
在与ClassView
一起使用时出现错误。因此,我使用了@method\u decorator
,在重定向到页面之前,它会将我带到登录页面。但是,我无法找到一种方法来限制对某些组的访问,例如使用我的类视图对管理员
、员工
、精简扫描
很少的帮助将被感激。谢谢 您可以在类视图中使用AccessMixin
我发现的例子:
from django.contrib.auth.mixins import AccessMixin
from django.http import HttpResponseRedirect
class FinanceOverview(AccessMixin, TemplateMixin):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
# This will redirect to the login view
return self.handle_no_permission()
if not self.request.user.groups.filter(name="FinanceGrp").exists():
# Redirect the user to somewhere else - add your URL here
return HttpResponseRedirect(...)
# Checks pass, let http method handlers process the request
return super().dispatch(request, *args, **kwargs)
更多信息请参见此处:您可以在类视图中使用AccessMixin
我发现的例子:
from django.contrib.auth.mixins import AccessMixin
from django.http import HttpResponseRedirect
class FinanceOverview(AccessMixin, TemplateMixin):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
# This will redirect to the login view
return self.handle_no_permission()
if not self.request.user.groups.filter(name="FinanceGrp").exists():
# Redirect the user to somewhere else - add your URL here
return HttpResponseRedirect(...)
# Checks pass, let http method handlers process the request
return super().dispatch(request, *args, **kwargs)
更多信息请参见此处:依赖可能是访问此类视图的一种简单得多的方法。您可以根据用户的组是否具有适当的权限,为这些组分配权限并授予对视图的访问权限,而不是检查特定的组列表
views.py
来自django.contrib.auth.decorators需要导入权限
从django.contrib.auth.mixin导入许可证所需的mixin
@需要权限('foo.view\u bar')
定义我的视图(请求):
...
类MyView(PermissionRequiredMixin,DetailView):
所需权限=('foo.view\u bar',)
...
依赖可能是访问此类视图的一种简单得多的方法。您可以根据用户的组是否具有适当的权限,为这些组分配权限并授予对视图的访问权限,而不是检查特定的组列表
views.py
来自django.contrib.auth.decorators需要导入权限
从django.contrib.auth.mixin导入许可证所需的mixin
@需要权限('foo.view\u bar')
定义我的视图(请求):
...
类MyView(PermissionRequiredMixin,DetailView):
所需权限=('foo.view\u bar',)
...
这很好,但是当我放入如果不是self.request.user.groups.filter(name=“Admin”,“Lite Scan”).exists():
它会发出一个错误,无法放入多个组,您可以使用和
`如果不是self.request.user.groups.filter(name=“Admin”)和self.request.user.groups.filter(name=“OtherGroup”)和
都不起作用,但你的建议让我在休息后放第二行,效果很好。谢谢你,伙计!这很好,但是当我放入如果不是self.request.user.groups.filter(name=“Admin”,“Lite Scan”).exists():
时,它会发出一个错误,无法放入多个组。您可以使用和
`如果不是self.request.user.groups.filter(name=“Admin”)和self.request.user.groups.filter(name=“OtherGroup”)和
都不起作用,但你的建议让我在休息后放第二行,效果很好。谢谢你,伙计!