I'm having my first go at a generic templatetag and am having issues
getting it to work.

I've created a directory at project/templatetags, which contains
__init__.py and general_tags.py
I've added project.templatetags to my INSTALLED_APPS in settings.py

general_tags.py looks like this (it's just a modification of the
default ifequal tag):

from django.template import Node, NodeList, resolve_variable
from django.template import TemplateSyntaxError, VariableDoesNotExist
from django.template import Library

register = Library()

class IfGreaterThanNode(Node):
    def __init__(self, var1, var2, nodelist_true, nodelist_false):
        self.var1, self.var2 = var1, var2
        self.nodelist_true, self.nodelist_false = nodelist_true,
nodelist_false
        self.negate = negate

    def __repr__(self):
        return "<IfGreaterThanNode>"

    def render(self, context):
        try:
            val1 = resolve_variable(self.var1, context)
        except VariableDoesNotExist:
            val1 = None
        try:
            val2 = resolve_variable(self.var2, context)
        except VariableDoesNotExist:
            val2 = None
        if (val1 > val2):
            return self.nodelist_true.render(context)
        return self.nodelist_false.render(context)

def do_ifgreaterthan(parser, token):
    bits = list(token.split_contents())
    if len(bits) != 3:
        raise TemplateSyntaxError, "%r takes two arguments" % bits[0]
    end_tag = 'end' + bits[0]
    nodelist_true = parser.parse(('else', end_tag))
    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse((end.tag,))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return IfEqualNode(bits[1], bits[2], nodelist_true,
nodelist_false)

def ifgreaterthan(parser, token):
    """
    Output the contents of the block if the first argument is greater
than the second argument.

    Examples:

        {% ifgreaterthan person.address_set.count 0 %}
            ...
        {% endifgreaterthan %}

        {% ifgreaterthan person.address_set.count 0 %}
            ...
        {% else %}
            ...
        {% endifgreaterthan %}
    """
    return do_ifgreaterthan(parser, token)
ifgreaterthan = register.tag(ifgreaterthan)

In my template, I have {% load general_tags %} and get the following
error:

TemplateSyntaxError at /project/admin/registration/choose_student/
'general_tags' is not a valid tag library: Could not load template
library from django.templatetags.general_tags, No module named
general_tags

Request Method: GET
Exception Type: TemplateSyntaxError
Exception Value:        'general_tags' is not a valid tag library: Could not
load template library from django.templatetags.general_tags, No module
named general_tags
Exception Location:     /usr/lib/python2.3/site-packages/django/template/
defaulttags.py in load, line 770

Django obviously isn't even looking in my templatetags directory for
the general_tags module.  Can anyone offer any advice?

Thanks!

Branton


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to