Stu*_*ter 2 django django-templates django-models django-views wagtail
我正在使用Django Wagtail构建一个站点,并且无法确定如何添加侧栏菜单,该菜单将列出父索引页面的所有子页面.例如,我有一个standard_index_page.html我在Admin中创建了一个父页面,然后我使用standard_page.html模板添加了它的子页面.
在我的standard_index_page.html模板中,我有以下代码
{% standard_index_listing calling_page=self %}
Run Code Online (Sandbox Code Playgroud)
它显示所有包含链接的子页面,但我也想显示子页面上所有子链接的列表.
我希望这是有道理的,有人可以伸出援助之手.谢谢.
本质上,您遍历由Django-Treebeard提供给Wagtail的页面层次结构的树结构.
许多前端框架不允许多级菜单,因为有些人认为它不属于最佳实践.但是,对于像SmartMenus这样的库,您可以使用一点肘部油脂来显示这种结构.
根据我的需要,没有简单的解决方案.所以,虽然我想分享一个我如何解决这个问题的例子,但可能缺少解释.如果您有任何疑问,我很乐意回答.
我挣扎了一段时间,虽然可能有更简单的方法来遍历树,但随着我的需求扩展,我构建了以下方法.它允许我们遍历我们网站中的所有实时页面,检查当前页面在菜单中的呈现时间,并允许对渲染进行细粒度控制.
这就是我们要做的事情:
在您的基本模板中,这意味着我们需要:
{% load demo_tags %} 导入我们的自定义模板标签{% top_menu calling_page=self %}以获取并呈现站点根目录的所有直接子级.这些是将在标准菜单栏中显示的项目.{% top_menu_children parent=menuitem %}在呈现的模板内调用{% top_menu %}以获取并呈现所有二级和更低级别的子页面.这包含悬停在父项菜单项上时要显示的所有菜单项.这是我创建的自定义demo_tags.py文件,用于遍历页面层次结构的所有级别.这样做的好处是它不需要提供任何自定义上下文数据; Wigtail开箱即用!
@register.assignment_tag(takes_context=True)
def get_site_root(context):
'''
Returns a core.Page, not the implementation-specific model used
so object-comparison to self will return false as objects would differ
'''
return context['request'].site.root_page
def has_menu_children(page):
'''
Returns boolean of whether children pages exist to the page supplied
'''
return page.get_children().live().in_menu().exists()
@register.inclusion_tag('info_site/tags/top_menu.html', takes_context=True)
def top_menu(context, parent, calling_page=None):
'''
Retrieves the top menu items - the immediate children of the parent page
The has_menu_children method is necessary in many cases. For example, a bootstrap menu requires
a dropdown class to be applied to a parent
'''
root = get_site_root(context)
try:
is_root_page = (root.id == calling_page.id)
except:
is_root_page = False
menuitems = parent.get_children().filter(
live=True,
show_in_menus=True
).order_by('title')
for menuitem in menuitems:
menuitem.show_dropdown = has_menu_children(menuitem)
return {
'calling_page': calling_page,
'menuitems': menuitems,
'is_root_page':is_root_page,
# required by the pageurl tag that we want to use within this template
'request': context['request'],
}
@register.inclusion_tag('my_site/tags/top_menu_children.html', takes_context=True)
def top_menu_children(context, parent, sub=False, level=0):
''' Retrieves the children of the top menu items for the drop downs '''
menuitems_children = parent.get_children().order_by('title')
menuitems_children = menuitems_children.live().in_menu()
for menuitem in menuitems_children:
menuitem.show_dropdown = has_menu_children(menuitem)
levelstr= "".join('a' for i in range(level)) # for indentation
level += 1
return {
'parent': parent,
'menuitems_children': menuitems_children,
'sub': sub,
'level':level,
'levelstr':levelstr,
# required by the pageurl tag that we want to use within this template
'request': context['request'],
}
Run Code Online (Sandbox Code Playgroud)
实质上,有三个级别的页面呈现:
{% get_site_root %}{% top_menu %}{% top_menu_children %},在呈现此标记时,菜单中显示的页面具有子级的任何时候都会调用它.为了做到这一点,我们需要创建一个到我们呈现模板top_menu和top_menu_children模板标签.
请注意 - 这些都是为Bootstrap 3的导航栏类而构建的,并根据我的需求进行了定制.只需根据您的需求定制这些.整个菜单构建过程都是通过调用{% top_menu_children %},因此将此标记放在您希望呈现菜单的基本模板中.更改top_menu.html以反映菜单的整体结构以及如何呈现每个菜单menuitem.更改children_items.html以反映您希望所有顶级菜单项的子项在任何深度呈现的方式.
my_site /标签/ top_menu.html
{% load demo_tags wagtailcore_tags static %}
{% get_site_root as site_root %}
{# FOR TOP-LEVEL CHILDREN OF SITE ROOT; In a nav or sidebar, these are the menu items we'd generally show before hovering. #}
<div class="container">
<div class="collapse navbar-collapse" id="navbar-collapse-3">
<ul class="nav navbar-nav navbar-left">
{% for menuitem in menuitems %}
<li class="{% if menuitem.active %}active{% endif %}">
{% if menuitem.show_dropdown %}
<a href="{{ menuitem.url }}">{{ menuitem.title }}
<span class="hidden-lg hidden-md hidden-sm visible-xs-inline">
<span class="glyphicon glyphicon-chevron-right"></span>
</span>
</a>
{% top_menu_children parent=menuitem %}
{% else %}
<a href="{% pageurl menuitem %}">{{ menuitem.title }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
my_site /标签/ children_items.html
{% load demo_tags wagtailcore_tags %}
{# For second- and lower-level decendents of site root; These are items not shown prior to hovering on their parent menuitem, hence the separate templates (and template tags) #}
<ul class="dropdown-menu">
{% for child in menuitems_children %}
{% if child.show_dropdown %}
<li>
<a href="{% pageurl child %}">
{% for i in levelstr %}  {% endfor %}
{{ child.title }}
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
{# On the next line, we're calling the same template tag we're rendering. We only do this when there are child pages of the menu item being rendered. #}
{% top_menu_children parent=child sub=True level=level %}
{# ^^^^ SmartMenus is made to render menus with as many levels as we like. Bootstrap considers this outside of best practices and, with version 3, has deprecated the ability to do so. Best practices are made to be broken, right :] #}
</li>
{% else %}
<li>
<a href="{% pageurl child %}">
<!-- Allows for indentation based on depth of page in the site structure -->
{% for i in levelstr %}  {% endfor %}
{{ child.title }}
</a>
</li>
{% endif %}
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
现在,在您的基本级别模板中(假设您正在使用一个;如果没有,请使用它:))您可以遍历菜单,同时保持杂乱清除您的inclusion_tags 使用的模板.
my_site/base.html文件
<ul class="nav navbar-nav navbar-left">
{% for menuitem in menuitems %}
<li class="{% if menuitem.active %}active{% endif %}">
{% if menuitem.show_dropdown %}
<a href="{{ menuitem.url }}">{{ menuitem.title }}
<span class="hidden-lg hidden-md hidden-sm visible-xs-inline">
<span class="glyphicon glyphicon-chevron-right"></span>
</span>
</a>
{% top_menu_children parent=menuitem %}
{% else %}
<a href="{% pageurl menuitem %}">{{ menuitem.title }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
我写了一篇关于此的博客文章 - 请查看更多细节.或者,请访问Thermaline.com以了解它的实际情况,但我认为现在没有多层次的深度.如果有,他们将自动呈现:)
现在,这个示例适用于导航栏,但它可以很容易地适用于侧边栏.
你需要做的就是:
demo_tags在您的基本模板中{% top_menu %}您希望呈现菜单的位置.top_menu.html并children_items.html呈现第一个页面和所有后续页面.在两级菜单上向Tivix致敬,这对我来说是一个很好的起点!
| 归档时间: |
|
| 查看次数: |
1142 次 |
| 最近记录: |