Я пытаюсь создать страницу обзора портфеля с помощью Django CMS.
У меня уже есть страницы с подробностями, и у них есть заполнитель с именем preview-img
. Для моей страницы обзора портфеля мне нужно перебрать все страницы с подробностями и отобразить этот заполнитель.
Документы Django говорят мне, что 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
?
Вот след для полноты:
Traceback:
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)