Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 2
    The href's properties are missing django template brackets {{, }}. For example, <a class="{% active request home %}" href="home">Home</a> should be, <a class="{% active request home %}" href="{{home}}">Home</a> the tags.py file will also need a few includes. Otherwise, great solution! Commented Sep 23, 2010 at 21:31
  • 3
    +1 This is more loosely coupled from applications. As a beginner I figured out tags needs it's own app, you can't just dump that into a global tags.py file. I created a new app called tags and everything went smoothly. docs.djangoproject.com/en/dev/howto/custom-template-tags Commented Jan 19, 2011 at 7:00
  • 3
    @Keyo, create a templatetags directory in your project, and add your project to installedapps. That'll also do the trick. Alternatively, like you said, create your main site as an app within your project. Commented Mar 22, 2011 at 22:38
  • 7
    Don't forget to add django.core.context_processors.request to your TEMPLATE_CONTEXT_PROCESSORS in settings.py Commented Sep 26, 2014 at 5:23
  • 1
    This is invalid for states that may be nested, e.g. mysite.com (as home) and mysite.com/blog, as the path will show as / and /blog/ (respectively) yielding a match for the former each time. If you don't use / as a landing, that may be okay otherwise I just use return 'active' if pattern == request.path else '' (I've not seen issues with this yet, but I just set up using this). Commented Mar 14, 2015 at 1:14