Django/Wagtail: 如何检查用户是否有权访问给定页面?
Django/Wagtail: How to check if the user has permissions to access a given Page?
我是 Wagtail 的新手,到目前为止这是一次很棒的体验!
我正在尝试解决以下问题:
用户可以看到所有可用子页面的列表,如果他没有访问每个页面的权限,它将显示一个储物柜图标,如附图所示。
Wireframe/sketch
我正在使用私人页面,特定组中的用户可以访问。
基本上,我有以下代码:
{% for course in page.get_children %}
<h2> <a href="{{ course.url }} "> {{ course.title }}
</a>
</h2>
{% endfor %}
是否有任何 属性 我可以检查用户是否对我的循环中的每个 course
具有或不具有权限?
我的模特:
from django.db import models
from wagtail.core.models import Page
# Create your models here.
class CoursePage(Page):
"""
A Page...
"""
description = models.TextField(
help_text='Text to describe the course',
blank=True)
subpage_types = ['course_module.ModulePage']
class ModulePage(Page):
description = models.TextField(
help_text='Text to describe the module',
blank=True)
subpage_types = ['lesson.LessonPage']
您将需要请求对象。这是一种方法:检查每个模块在 get_context
中的查看限制,并将模块列表和权限添加到上下文中。
class CoursePage(Page):
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
modules_list = []
for module in self.get_children():
restrictions = module.get_view_restrictions()
auth = all(restriction.accept_request(request)
for restriction in restrictions)
modules_list.append((module, auth))
context['modules_list'] = modules_list
return context
然后在您的模板中,您可以使用授权标志来确定是否应显示锁定图标。
我是 Wagtail 的新手,到目前为止这是一次很棒的体验!
我正在尝试解决以下问题: 用户可以看到所有可用子页面的列表,如果他没有访问每个页面的权限,它将显示一个储物柜图标,如附图所示。
Wireframe/sketch
我正在使用私人页面,特定组中的用户可以访问。
基本上,我有以下代码:
{% for course in page.get_children %}
<h2> <a href="{{ course.url }} "> {{ course.title }}
</a>
</h2>
{% endfor %}
是否有任何 属性 我可以检查用户是否对我的循环中的每个 course
具有或不具有权限?
我的模特:
from django.db import models
from wagtail.core.models import Page
# Create your models here.
class CoursePage(Page):
"""
A Page...
"""
description = models.TextField(
help_text='Text to describe the course',
blank=True)
subpage_types = ['course_module.ModulePage']
class ModulePage(Page):
description = models.TextField(
help_text='Text to describe the module',
blank=True)
subpage_types = ['lesson.LessonPage']
您将需要请求对象。这是一种方法:检查每个模块在 get_context
中的查看限制,并将模块列表和权限添加到上下文中。
class CoursePage(Page):
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
modules_list = []
for module in self.get_children():
restrictions = module.get_view_restrictions()
auth = all(restriction.accept_request(request)
for restriction in restrictions)
modules_list.append((module, auth))
context['modules_list'] = modules_list
return context
然后在您的模板中,您可以使用授权标志来确定是否应显示锁定图标。