【问题标题】:give an active to class to active link给类到活动链接一个活动
【发布时间】:2010-12-25 16:50:55
【问题描述】:

我正在编写一个建立在 django 框架背后的 python 网站,我正在寻找一种方法来突出显示用户依赖于 URL 的当前链接,我认为这样做会起作用。

我所做的是创建一个名为nav 的新应用程序并构建了一些模板标签,就像这样,

from django import template

register = template.Library()

URL_PATTERNS = {
    'home': (r'^/$',),
}

@register.tag
def nav_selection(parser, token):
    try:
        tag_name, nav_item = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
    if not (nav_item[0] == nav_item[-1] and nav_item[0] in ('"', "'")):
        raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
    return NavSelectionNode(nav_item[1:-1])

class NavSelectionNode(template.Node):
    def __init__(self, nav_item):
        self.nav_item = nav_item
    def render(self, context):
        if not 'request' in context:
          return "" 
        import re
        try:
            regs = URL_PATTERNS[self.nav_item]
        except KeyError:
            return ''
        for reg in regs:
            if re.match(reg, context['request'].get_full_path()):
                return "active"
        return ''

在我的模板中我这样做

<ul id="navigation">{% load nav %}
                <li><a href="{% url views.home %}" class='{% nav_selection "home" %}'>home</a></li>
                <li><a href="{% url views.about %}" class='{% nav_selection "about" %}'>about neal &amp; wolf</a></li>
                <li><a href="{% url shop.views.home %}" class='{% nav_selection "shop" %}'>our products</a></li>
                <li><a href="{% url shop.views.home %}" class='{% nav_selection "shop" %}'>shop</a></li>
                <li><a href="{% url views.look %}" class='{% nav_selection "look" %}'>get the look</a></li>
                <li><a href="{% url news.views.index %}" class='{% nav_selection "news" %}'>news</a></li>
                <li><a href="{% url contact.views.contact %}" class='{% nav_selection "contact" %}'>contact us</a></li>
                <li><a href="{% url store_locator.views.index %}" class='{% nav_selection "finder" %}'>salon finder</a></li>
                <li><a href="{% url professional.views.index %}" class='{% nav_selection "contact" %}'>neal &amp; wolf professional</a></li>

            </ul>

然而,我在 firebug 中得到的标记是这个例子,我正在浏览索引页面

<a class="" href="/home/">

所以显然有些事情失败了,但我看不到哪里失败,有人可以帮我吗?

【问题讨论】:

    标签: python django django-templates templatetags


    【解决方案1】:

    需要检查的一些事项:

    request 对象实际上是否在您的上下文中?您是专门传递它,还是使用RequestContext

    为什么要在模板标签中定义正则表达式,而不是使用内置的reverse 函数在 urlconf 中查找它们?

    这里的正则表达式是否真的与 urlconf 中的匹配?

    您是否以某种方式将您的home urlconf 包含在“主页”网址下?

    【讨论】:

    • 对不起,我对python真的很厚,只做了大约2周,这是正确的方法吗?这不是我的代码,我继承了它,有没有更简单的方法?如何查看请求对象中的内容?当我 render_to_response 时,我的最后一个参数是 RequestContext,这就是你的意思吗?对不起,我很笨。
    猜你喜欢
    • 2017-05-16
    • 2017-09-30
    • 2015-04-03
    • 2017-11-26
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    相关资源
    最近更新 更多