Wagtail-In django如何从页面id获取页面属性
Wagtail-In django how to get page attributes from page id
我从 url
获取 wagtail 页面 ID
https://www.example.com/results?id=14&id=15
在views.py
def get_data(request, **kwargs):
resources = request.GET.getlist('id')
如何借助页面id获取相关的页面属性?
在models.py
context['selected_resources'] = ??
这样我就可以像这样在模板中渲染它
在results.html
% for resource in selected_resources %}
<p>{{resource.title}}</p>
<p>{{resource.description}}</p>
{% endfor %}
使用filter
with an id__in
lookup:
resource_ids = request.GET.getlist('id')
context['selected_resources'] = ResourcePage.objects.filter(id__in=resource_ids)
这假定您在 URL 中传递的 ID 始终引用相同的页面类型(本例中为 ResourcePage
)。这可能是一个安全的假设 - 如果页面类型混合,您不能真正保证它们都有一个 description
字段,因此您不能在输出中做任何有用的事情。但是如果你确实需要处理多种不同的页面类型(它们都定义了 description
),你可以按如下方式进行(以一些额外的数据库查询为代价):
context['selected_resources'] = Page.objects.filter(id__in=resource_ids).specific()
我从 url
获取 wagtail 页面 IDhttps://www.example.com/results?id=14&id=15
在views.py
def get_data(request, **kwargs):
resources = request.GET.getlist('id')
如何借助页面id获取相关的页面属性?
在models.py
context['selected_resources'] = ??
这样我就可以像这样在模板中渲染它
在results.html
% for resource in selected_resources %}
<p>{{resource.title}}</p>
<p>{{resource.description}}</p>
{% endfor %}
使用filter
with an id__in
lookup:
resource_ids = request.GET.getlist('id')
context['selected_resources'] = ResourcePage.objects.filter(id__in=resource_ids)
这假定您在 URL 中传递的 ID 始终引用相同的页面类型(本例中为 ResourcePage
)。这可能是一个安全的假设 - 如果页面类型混合,您不能真正保证它们都有一个 description
字段,因此您不能在输出中做任何有用的事情。但是如果你确实需要处理多种不同的页面类型(它们都定义了 description
),你可以按如下方式进行(以一些额外的数据库查询为代价):
context['selected_resources'] = Page.objects.filter(id__in=resource_ids).specific()