Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 497827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:49:08+00:00 2026-05-13T05:49:08+00:00

I am writing a python website built on the back of the django framework,

  • 0

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'^/$',),
}

@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

<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>

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:49:08+00:00Added an answer on May 13, 2026 at 5:49 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a python script that executes a csh script in Solaris 10. The
I'm writing a Python application that takes a command as an argument, for example:
I'm writing a Python program with a lot of file access. It's running surprisingly
I am writing a Python utility that needs to parse a large, regularly-updated CSV
I'm writing a mapping application that I am writing in python and I need
I'm coding the menu for an application I'm writing in python, using wxPython libraries
So I've decided to try to solve my physics homework by writing some python
I am writing a simple Python web application that consists of several pages of
When I am writing code in Python, I often need to remove items from
In the python program I'm writing, I've got a thread which iterates over a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.