1
如何在base.html中的Django CMS中使用条件来检测页面是否为主页并向body标签添加唯一类?我宁愿不重复基地,只是添加一个类,以便我可以在主页上以不同方式处理一些样式。Django CMS有条件
如何在base.html中的Django CMS中使用条件来检测页面是否为主页并向body标签添加唯一类?我宁愿不重复基地,只是添加一个类,以便我可以在主页上以不同方式处理一些样式。Django CMS有条件
这取决于你如何构建你的网页。
我选择将页面创建为“主页”页面的子项,因此请使用类似于页面标题的内容;
{% if request.current_page.get_ancestors|length <= 1 %}
<h1>{{ request.current_page.get_page_title }}</h1>
{% else %}
{% for ance in request.current_page.get_ancestors %}
{% if ance.depth == 2 %}
<h1>{{ ance.get_page_title }}</h1>
{% endif %}
{% endfor %}
{% endif %}
所以你可以做一些类似的事情;
<body class="{% if request.current_page.get_ancestors|length <= 1 %}base{% endif %}">
这正是我所需要的。谢谢! –
@DebbieGray没问题,高兴帮忙:) –