【问题标题】:how to aumatically select menu item in the base template for each section of the main page in django template system?如何在django模板系统中为主页的每个部分自动选择基本模板中的菜单项?
【发布时间】:2011-02-23 10:04:30
【问题描述】:

我的项目中有几个部分具有不同的页面模板,例如 A.html、B.html、C.html。它们都扩展了 base.html。我想在 base.html (页眉)上显示一个导航菜单,并以不同的方式显示当前页面项,即。 class=为 A.html 页面上的 A 链接附加了“已选择”。

我怎样才能达到这个效果?是否可以从基础模板中获取模板的名称?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您可以将当前项目的名称放在上下文中,并将其传递给模板。在模板中,检查是否为选中项:

    <div class='nav'>
        <a {% ifequal selected 'about' %}class='selected'{% endifequal %} href='/about/'>About</a>
        ...
    </div>
    

    其他解决方案可以参考这篇帖子Django templates - can I set a variable to be used in a parent template?

    【讨论】:

    • 我不想为此使用上下文。在模板系统中应该是可能的..
    猜你喜欢
    • 1970-01-01
    • 2011-04-29
    • 2011-12-15
    • 2012-01-13
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2011-11-13
    • 2017-11-20
    相关资源
    最近更新 更多