Snippet List
I needed a way to find if a menu items should be active. After searching the internet i found a few options*, but none of them did fit my needs, so i wrote my own:
Usage:
<a href="{% url 'view-name' %}" class="{% current request 'view-name' %}"></a>
* http://gnuvince.wordpress.com/2008/03/19/the-new-and-improved-active-tag/
* http://stackoverflow.com/questions/340888/navigation-in-django
- template
- path
- active
- link
- current
Active class for navigation link
{% navclass default current url %}
First argument is the default class and second is the current class
Third argument is the url name(s) of the page
example: <a class="{% navclass leftnav curentnav name1,name2 %}" href="/about/">
- css
- style
- navigation
- active
This module defines a template tag `{% ifactive %}` that lets you determine which page is currently active. A common use case is for highlighting the current page in a navigation menu.
It uses the same syntax for specifying views as the `{% url %}` tag, and determines whether a particular page is active by checking if the same view is called with the same arguments, instead of just comparing URLs. As a result, it can handle cases where different URLs map to the same view.
Example usage:
<a {% ifactive request page1 %}class='active'{% endifactive %}
href='{% url page1 %}'>Page 1</a>
<a {% ifactive request page2 %}class='active'{% endifactive %}
href='{% url page2 %}'>Page 2</a>
...
<a {% ifactive request pageN %}class='active'{% endifactive %}
href='{% url pageN %}'>Page N</a>
It also can be extended to further reduce the amount of repetitive code. For instance, you could write a template tag that has class='active' as the block contents, and always gets the variable from context['request']:
def do_activeif(parser, token):
"e.g. <a {% activeif page1 %} href='{% url page1 %}'>Page 1</a>"
tag_args = token.contents.split(' ')
view_name = tag_args[1]
args, kwargs = _parse_url_args(parser, tag_args[2:])
return ActiveNode('request', view_name, args, kwargs, NodeList(TextNode('class="active"')))
register.tag('activeif', do_activeif)
Note that you will have to add the ActiveViewMiddleware to your settings.py:
MIDDLEWARE_CLASSES = (
...,
'path.to.this.module.ActiveViewMiddleware'
)
You may also want to add this template tag as a built-in, so you don't have
to call `{% load %}`. In somewhere that's imported by default (e.g. where you
define your views), add:
from django import template
template.add_to_builtins('path.to.this.module')
- template
- tag
- page
- active
- ifactive
This manager is intended for use with models with publication and/or expiration dates. Objects will be retrieved only if their publication and/or expiration dates are within the current date.
Use is very simple:
class ExampleModel(models.Model):
publish_date = models.DateTimeField()
expire_date = models.DateTimeField(blank=True, null=True)
objects = models.Manager()
actives = ActiveManager(from_date='publish_date', to_date='expire_date')
ExampleModel.actives.all() # retrieve active objects according to the current date
The manager works correctly with nullable date fields. A null publication date means "*always published (until expiration date)*" and a null expiration date means "*never expires*".
Most models should define the `objects` manager as the default manager, because otherwise out of date objects won't appear in the admin app.
- model
- manager
- active
- publication
- expiration
- date-filter
7 snippets posted so far.