我正在尝试使用Django CMS创建投资组合概述页面。
我已经有我的详细信息页面,并且它们有一个占位符,称为preview-img
。对于我的投资组合概述页面,我需要遍历所有详细信息页面并呈现该占位符。
Django docs告诉我inclusion tag
是行之有效的方法。因此,我在show_portfolio
目录中定义了标签(templatetags
),以便其返回页面:
from django import template
from cms.models.pagemodel import Page
from django.template import RequestContext
register = template.Library()
@register.inclusion_tag('portfolio_item.html', takes_context=True)
def show_portfolio(context, parent_slug):
cms_pages = Page.objects.public()
cms_pages = [
page for page in cms_pages
if page.parent_page and page.parent_page.get_slug() == parent_slug
]
return {
'cms_pages': cms_pages,
}
它只是返回带有给定父页面的页面对象(通过slug)。
我现在在cms_pages
上循环的模板是这个(请注意尝试渲染每个页面的preview
show_placeholder
):
[portfolio_item.html]
{% load cms_tags %}
<div class="row">
{% for page in cms_pages %}
...
{% show_placeholder "preview-img" page %}
...
{% endfor %}
</div>
运行此命令,服务器在尝试评估KeyError
时遇到未捕获的request
(键:show_placeholder
)。我看不到与我正在做的事情的联系。该请求在哪里起作用?甚至可以在show_placeholder
的模板中使用inclusion tag
吗?
这是完整性的踪迹:
跟踪:
File ".../python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File ".../python3.6/site-packages/django/core/handlers/base.py" in _get_response
217. response = self.process_exception_by_middleware(e, request)
File ".../python3.6/site-packages/django/core/handlers/base.py" in _get_response
215. response = response.render()
File ".../python3.6/site-packages/django/template/response.py" in render
107. self.content = self.rendered_content
File ".../python3.6/site-packages/django/template/response.py" in rendered_content
84. content = template.render(context, self._request)
File ".../python3.6/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File ".../python3.6/site-packages/django/template/base.py" in render
207. return self._render(context)
File ".../python3.6/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File ".../python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File ".../python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File ".../python3.6/site-packages/classytags/core.py" in render
153. return self.render_tag(context, **kwargs)
File ".../python3.6/site-packages/sekizai/templatetags/sekizai_tags.py" in render_tag
93. rendered_contents = nodelist.render(context)
File ".../python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File ".../python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File ".../python3.6/site-packages/classytags/core.py" in render
153. return self.render_tag(context, **kwargs)
File ".../python3.6/site-packages/cms/templatetags/cms_tags.py" in render_tag
443. return toolbar.render_with_structure(context, nodelist)
File ".../python3.6/site-packages/cms/toolbar/toolbar.py" in render_with_structure
498. rendered_contents = nodelist.render(context)
File ".../python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File ".../python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File ".../python3.6/site-packages/django/template/library.py" in render
245. return t.render(new_context)
File ".../python3.6/site-packages/django/template/base.py" in render
209. return self._render(context)
File ".../python3.6/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File ".../python3.6/site-packages/django/template/base.py" in render
990. bit = node.render_annotated(context)
File ".../python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File ".../python3.6/site-packages/django/template/defaulttags.py" in render
216. nodelist.append(node.render_annotated(context))
File ".../python3.6/site-packages/django/template/base.py" in render_annotated
957. return self.render(context)
File ".../python3.6/site-packages/django/template/library.py" in render
203. output = self.func(*resolved_args, **resolved_kwargs)
File ".../python3.6/site-packages/cms/templatetags/cms_tags.py" in _show_placeholder_by_id
116. request = context['request']
File ".../python3.6/site-packages/django/template/context.py" in __getitem__
87. raise KeyError(key)
答案 0 :(得分:1)
通过在包含标记的上下文中显式传递请求,我可以在测试设置中正常工作
from django import template
from cms.models.pagemodel import Page
register = template.Library()
@register.inclusion_tag('portfolio_item.html', takes_context=True)
def show_portfolio(context, parent_slug):
cms_pages = Page.objects.public()
cms_pages = [
page for page in cms_pages
if page.parent_page and page.parent_page.get_slug() == parent_slug
]
return {
'request': context.request,
'cms_pages': cms_pages,
}