1

I want to take the URL from request.path in a template and do a test on just the first word. If my URL is this:

/estimates/commercial

I want to somehow remove the commercial from the request.path. I was hoping this is possible within the template, too, since I need to do this check at every page.

Is this possible within my Django template?

EDIT - for clarification

The purpose of this is to highlight the navigation link based on what page is currently being viewed. I have a main nav and a sub nav and I want to highlight as follows:

main nav           --> [Systems][Estimates]
"Systems" sub nav ---> [New][Details][Invoives]

If I am in the Details section of the Systems section, I want the words System and Details to be a different colour, or underlined, or something.

Garfonzo
  • 3,876
  • 6
  • 43
  • 78

3 Answers3

4

Your options are:

  1. Write a ContextProcessor

  2. Write a custom template filter. I'd reccomend just writing a split template filter and then using the join and slice filters to get the desired affect. However, it might be easier to just write a filter that does the whole thing.

Truthfully I'd consider #1 to be the better option. You are using context processors already right? If not, now is the time =)

def somename(request):
    return { 'some_context_var': request.path.split('/')[:-1] }

Edit:

If your path was /something/foo/bar/estimates/commercial/

def somename(request):
    primary, secondary  = request.path.split('/')[-2:]
    return { 'primary_name': primary, 'secondary_name': secondary }

would give you 2 context vars with 'estimates' and 'commercial' as their values. This idea is pretty easy to expand, or even make more abstract and allow an arbitrary number of context variables being added.

John
  • 5,166
  • 27
  • 31
  • Option 1. works like a charm for my main navigation bar. My urls always have the main nav name as the first item in the url path. Thus, a context processor worked perfectly. However, highlighting the second level of navigation is quite a bit trickier and I think a filter would be appropriate. However, doesn't a filter need to reside within an app directory? I have several apps within one project which make up the whole web-app. It seems inefficient to reproduce the filter code within every single app. Perhaps I'm wrong on that? – Garfonzo Oct 09 '11 at 17:44
  • A filter as the below suggest just resides in one locations and you load it by name in any template that needs it. However, a context processor is likely still the best option. Though you will need to think about your exact case and apply it. If your navigation tiers are always the last 2 sections of your url you could right it as I have in my edit. – John Oct 10 '11 at 00:30
1

You could use django-treemenus, which can be extended in many ways. In the manual, there is also an example how to highlight a navigation item if the user is within a certain path.

schneck
  • 10,556
  • 11
  • 49
  • 74
0

Without writing your own custom template filter, it'd be easier, I think, to do this in the view code and pass the relevant portion into the template.

imm
  • 5,837
  • 1
  • 26
  • 32