提供一个活动到班级到活动的链接

发布于 2024-08-13 21:59:33 字数 2699 浏览 6 评论 0原文

我正在编写一个基于 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/">

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

I am writing a python website built on the back of the django framework, I am looking for a way to highlight the current link the user is on depening on what the URL, I thought doing some thing like this would work.

What I have done is create a new application called nav and built some templatetags, like so,

from django import template

register = template.Library()

URL_PATTERNS = {
    'home': (r'^/

In my template I do this

<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 & 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 & wolf professional</a></li>

            </ul>

yet the markup I get out in firebug is this in this example I am browsing the index page

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

So something is obviously failing but I cannot see where, can anyone help me please?

,), } @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 ''

In my template I do this

yet the markup I get out in firebug is this in this example I am browsing the index page

So something is obviously failing but I cannot see where, can anyone help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-08-20 21:59:33

需要检查的一些事项:

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

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

这里的正则表达式实际上与 urlconf 中的正则表达式匹配吗?

您是否以某种方式将您的 home urlconf 包含在“home”url 下?

Some things to check:

Is the request object actually in your context? Are you passing it in specifically, or are you using a RequestContext?

Why are you defining regexes in your templatetags, rather than using the built-in reverse function to look them up in the urlconf?

Do the regexes here actually match the ones in the urlconf?

Have you included your home urlconf under the 'home' url somehow?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文