I'm guessing it's somewhat common (as usual, I think it's common
because I do it) to put several navigational links in a template that
is included from several other templates, e.g.:

<a href='{% url page1 %}'>Page 1</a>
<a href='{% url page2 %}'>Page 2</a>
...
<a href='{% url pageN %}'>Page N</a>

But also, it's helpful to indicate where the user currently is in the
navigational hierarchy, e.g.:

<a href='{% url page1 %}'>Page 1</a>
<a class='active' href='{% url page1 %}'>Page 2</a>
...
<a href='{% url pageN %}'>Page N</a>

With Django I don't know of a convenient built-in way to do this. One
could copy and paste N slightly-different versions of the navigational
links template, but that isn't maintainable. One could manually pass
in an extra context parameter in each of the N views, and add {% if %}
tests in the template for each of them, but that's tedious.

What I want is the ability to write a custom template tag that uses
the same parameters as the tag {% url pageN %}, but which acts as a
conditional and does something different if the parameter matches the
currently active urlpattern. E.g., I could write a block tag {%
ifactive pageN %}:

<a {% ifactive page1 %}class='active'{% endifactive %} href='{% url
page1 %}'>Page 1</a>
<a {% ifactive page2 %}class='active'{% endifactive %} href='{% url
page2 %}'>Page 2</a>
...
<a {% ifactive pageN %}class='active'{% endifactive %}  href='{% url
pageN %}'>Page N</a>

It doesn't seem like this can be done with standard Django because the
request doesn't have a reference to the URL pattern that was actually
used. My idea (implemented in my local version of Django) was to
modify RegexURLResolver to return the urlpattern that was used, and
store that (as well as the kwargs) as a property of the request.

Does this seem like a useful patch? Or is there a better way to
accomplish this?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to