【问题标题】:Django: Is there a better way to bold the current page linkDjango:有没有更好的方法来加粗当前页面链接
【发布时间】:2010-11-04 16:21:05
【问题描述】:

我有一个包含链接列表的 base.html 模板。

例子:

   <div id="sidebar1">
        <ul>
        <li><a href="/" title="">Index</a></li>
        <li><a href="/stuff/" title="" class="current">Stuff</a></li>
        <li><a href="/about/" title="">About Me</a></li>
        <li><a href="/contact/" title="">Contact Me</a></li>
    </div>

然后我在views.py 中定义了index.html、stuff.html、about.html 和contact.html。这些模板中的每一个都简单地从 base.html 模板派生并设置各自的标题和内容。

我的问题是关于上述/stuff 我有一个 class="current"。

我想让我所在的当前页面具有该类属性。

我可以在每个视图中设置一个不同的变量,例如 current_page="about",然后在模板中与每个链接的每个类元素中的 {% ifequal %} 进行比较,但这似乎是重复工作(因为额外的视图变量)。

有没有更好的方法?也许如果有一种方法可以获取自动填充模板的视图函数名称,我不需要设置额外的变量?此外,它似乎确实有很多 ifequals。

【问题讨论】:

    标签: django django-templates django-urls


    【解决方案1】:

    这是一种优雅的方法,我从某个地方复制了它,我只希望我能记住在哪里,这样我就可以给他们功劳了。 8-)

    我为我的每个页面(或一个部分中的所有页面)分配一个id,如下所示:

    In index.html:    <body id='section-intro'>...
    In faq.html:      <body id='section-faq'>...
    In download.html: <body id='section-download'>...
    

    然后是对应链接的id

    <li id='nav-intro'><a href="./">Introduction</a></li>
    <li id='nav-faq'><a href="./faq.html">FAQ</a></li>
    <li id='nav-download'><a href="./download.html">Download</a></li>
    

    在 CSS 中我设置了这样的规则:

    #section-intro #nav-intro,
    #section-faq #nav-faq,
    #section-download #nav-download {
        font-weight: bold;
        /* And whatever other styles the current link should have. */
    }
    

    因此,这主要以声明性方式控制当前页面所属链接的样式。您可以在此处查看它的实际效果:http://entrian.com/source-search/

    一旦你设置了它,它就是一个非常干净和简单的系统,因为:

    • 您无需在链接中使用模板标记
    • 您最终不会使用又大又丑的switch 语句或if / else / else 语句
    • 将页面添加到 Just Works [TM] 部分中
    • 改变事物的外观只意味着改变 CSS,而不是标记。

    我没有使用 Django,但是这个系统可以在任何地方使用。在您的情况下,您“设置各自的标题和内容”还需要设置 body id,并且不需要其他 Django 标记。

    这个想法也很容易扩展到其他情况,例如。 “除了下载页面本身,我希望每个页面的侧边栏中都有一个下载链接。”你可以在 CSS 中这样做:

    #section-download #sidebar #download-link {
        display: none;
    }
    

    而不必在侧边栏 HTML 中放置条件模板标记。

    【讨论】:

    • 这不是意味着 CSS 中有很多重复的内容吗?
    • ...我的意思是每个链接 id 的样式。
    • 每个页面/部分只有一行 CSS。样式本身只出现一次,前面是逗号分隔的部分/导航对列表。
    • 我首先从这里发现了这种技术:24ways.org/2005/auto-selecting-navigation
    【解决方案2】:

    没有使用过 Django,但我在 Kohana (PHP) 和 Rails 中处理过同样的问题。

    我在 Kohana 做什么:

    <li><a href="/admin/dashboard" <?= (get_class($this) == 'Dashboard_Controller') ? "class=\"active\"" : NULL ?>>Dashboard</a></li>
    <li><a href="/admin/campaigns" <?= (get_class($this) == 'Campaigns_Controller') ? "class=\"active\"" : NULL ?>>Campaigns</a></li>
    <li><a href="/admin/lists" <?= (get_class($this) == 'Lists_Controller') ? "class=\"active\"" : NULL ?>>Lists</a></li>
    

    我在 Rails 中的工作:

    <li><a href="/main" <%= 'class="active"' if (controller.controller_name == 'main') %>>Overview</a></li>
    <li><a href="/notifications" <%= 'class="active"' if (controller.controller_name == 'notifications') %>>Notifications</a></li>
    <li><a href="/reports" <%= 'class="active"' if (controller.controller_name == 'reports') %>>Reports</a></li>
    

    【讨论】:

      【解决方案3】:

      我只看到了几种方法,同时避免重复 ifequals:

      1. Javascript。类似于(jQuery)的东西:

        var parts = window.location.pathname.split('/');
        var page = parts[parts.length-1];
        $('#sidebar1 a[href*=' + page + ']').addClass('current');
        
      2. 更改您的视图以包含页面列表及其相关标题和 URL,并在您的模板中创建一个 {% for %} 循环,该循环将遍历该列表,并添加一个 {% ifequal %} .

      选项 2 是我的最爱。如果所有页面的逻辑都相同,只是模板不同,则可以考虑为每个页面使用 FlatPages 模型。如果逻辑不同,并且您需要不同的模型,则可以考虑使用某种菜单应用程序。无耻的外挂:我有menuing app of my own

      【讨论】:

        【解决方案4】:

        如果您添加 request 上下文处理器,则非常简单:

        settings.py:
        
        TEMPLATE_CONTEXT_PROCESSORS = (
            'django.core.context_processors.request',
            'django.contrib.auth.context_processors.auth'  # admin app wants this too
        )
        

        现在您可以访问包含请求路径的HttpRequest。突出显示当前页面是检查路径是否与链接的目标匹配的简单问题,即您已经在那里:

        <li><a class="{% if request.path == '/' %}current{% endif %}" href="/">Index</a></li>
        <li><a class="{% if request.path == '/stuff/' %}current{% endif %}" href="/stuff/">Stuff</a></li>
        <li><a class="{% if request.path == '/about/' %}current{% endif %}" href="/about/">About Me</a></li>
        <li><a class="{% if request.path == '/contact/' %}current{% endif %}" href="/contact/">Contact Me</a></li>
        

        【讨论】:

          猜你喜欢
          • 2011-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-03
          • 2013-04-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多