在 Wagtail 中获取块中的当前用户 class
Getting current user in blocks class in Wagtail
我正在尝试使用 Wagtail 2.9 创建一个具有生成 URL 函数的块。要生成 URL 我需要当前登录的用户。
class Look(blocks.StructBlock):
title = blocks.CharBlock(required=True, help_text='Add your look title')
id = blocks.CharBlock(required=True, help_text='Enter the Id')
class Meta:
template = "looker/looker_block.html"
value_class = LookStructValue
值 class 的 get_url()
定义如下:
class LookStructValue(blocks.StructValue):
def url(self):
id = self.get('id')
user = User(15,
first_name='This is where is need the current user First name',
last_name='and last name',
permissions=['some_permission'],
models=['some_model'],
group_ids=[2],
external_group_id='awesome_engineers',
user_attributes={"test": "test",
"test_count": "1"},
access_filters={})
url_path = "/embed/looks/" + id
url = URL(user,url_path, force_logout_login=True)
return "https://" + url.to_string()
我可以在 LookStructValue class 中获取当前用户吗?
您可以使用 blocks.Structblock 的 get_context
方法访问父级的上下文 (parent_context
)。
确保使用 {% include_block %}
渲染块。
The parent_context keyword argument is available when the block is rendered through an {% include_block %}
tag, and is a dict of variables passed from the calling template.
您必须重新考虑如何创建用户的 URL,而不是将其移动到用户模型上的方法(例如:create_custom_url
)。
# Basic Example, to point you the right way.
class DemoBlock(blocks.StructBlock):
title = blocks.CharBlock()
def get_context(self, value, parent_context=None):
"""Add a user's unique URL to the block's context."""
context = super().get_context(value, parent_context=parent_context)
user = parent_context.get('request').user
context['url'] = user.create_custom_url()
return context
我正在尝试使用 Wagtail 2.9 创建一个具有生成 URL 函数的块。要生成 URL 我需要当前登录的用户。
class Look(blocks.StructBlock):
title = blocks.CharBlock(required=True, help_text='Add your look title')
id = blocks.CharBlock(required=True, help_text='Enter the Id')
class Meta:
template = "looker/looker_block.html"
value_class = LookStructValue
值 class 的 get_url()
定义如下:
class LookStructValue(blocks.StructValue):
def url(self):
id = self.get('id')
user = User(15,
first_name='This is where is need the current user First name',
last_name='and last name',
permissions=['some_permission'],
models=['some_model'],
group_ids=[2],
external_group_id='awesome_engineers',
user_attributes={"test": "test",
"test_count": "1"},
access_filters={})
url_path = "/embed/looks/" + id
url = URL(user,url_path, force_logout_login=True)
return "https://" + url.to_string()
我可以在 LookStructValue class 中获取当前用户吗?
您可以使用 blocks.Structblock 的 get_context
方法访问父级的上下文 (parent_context
)。
确保使用 {% include_block %}
渲染块。
The parent_context keyword argument is available when the block is rendered through an
{% include_block %}
tag, and is a dict of variables passed from the calling template.
您必须重新考虑如何创建用户的 URL,而不是将其移动到用户模型上的方法(例如:create_custom_url
)。
# Basic Example, to point you the right way.
class DemoBlock(blocks.StructBlock):
title = blocks.CharBlock()
def get_context(self, value, parent_context=None):
"""Add a user's unique URL to the block's context."""
context = super().get_context(value, parent_context=parent_context)
user = parent_context.get('request').user
context['url'] = user.create_custom_url()
return context