Hello, this may be a bug but I was sent here.  I have a django project
with three apps. There are
three apps to seperate out kinds of data models.  However the views of
the apps build on each
other.

The documentation implies you can define your own filters but does not
detail how this is done so I
started going through the source code.

You do this, apparently, by creating a sub-dir in your app called
"templatetags" (with __init__.py so
it gets recognized as a module.) In this directory you put a python
file which has functions
that match the signature for filters (see django.core.defaultfilters)
and after you have defined
your filters you invoke:

   template.register_filter('random', random, False)

(template.* comes from an import at the top: import template

(The third parameter is if you filter takes an argument or not.)

Then in your template you can do:

   {% load <app>.<templatetags-file-module-name> %}

Accordingto what I could figure out it looks for "app" in your list of
installed apps and knows
to look in the <app>/templatetags/ dir for the module that you want to
load.

I did this for the custom filter I made. Put it in the template and it
worked, on the dev server.
However, when I set it up in mod python it failed spectacularly.

Other views (that do not call that load statement and thus do not use
that filter) work fine.

One tricky thing is I have the filter defined in one app since I
thought it was more appropiate
to that app, and I am using it in a template in a different app.

The failure I get when loading the page that is served via
apache/mod_python looks like:

There's been an error:

Traceback (most recent call last):

  File
"/usr/local/lib/python2.4/site-packages/django/core/handlers/base.py",
line 64, in get_response
    response = callback(request, **param_dict)

  File "/usr/local/www/wowzer/wowzer/apps/madhouse/views/auction.py",
line 72, in detail
    t = template_loader.get_template('madhouse/detail')

  File
"/usr/local/lib/python2.4/site-packages/django/core/template_loader.py",
line 13, in get_template
    return
get_template_from_string(load_template_source(template_name))

  File
"/usr/local/lib/python2.4/site-packages/django/core/template_loader.py",
line 20, in get_template_from_string
    return template.Template(source)

  File
"/usr/local/lib/python2.4/site-packages/django/core/template.py", line
106, in __init__
    self.nodelist = compile_string(template_string)

  File
"/usr/local/lib/python2.4/site-packages/django/core/template.py", line
121, in compile_string
    return parser.parse()

  File
"/usr/local/lib/python2.4/site-packages/django/core/template.py", line
219, in parse
    nodelist.append(registered_tags[command](self, token))

  File
"/usr/local/lib/python2.4/site-packages/django/core/defaulttags.py",
line 602, in do_load
    LoadNode.load_taglib(taglib)

  File
"/usr/local/lib/python2.4/site-packages/django/core/defaulttags.py",
line 228, in load_taglib
    mod = __import__("django.templatetags.%s" % taglib.split('.')[-1],
'', '', [''])

  File
"/usr/local/www/wowzer/wowzer/apps/items/templatetags/helpers.py", line
64, in ?
    django.core.template.register_filter('num_to_gold', num_to_gold,
True)

AttributeError: 'module' object has no attribute 'core'

The filter is 'num_to_gold' -- the error is happening on the line where
I try to register the filter
Now I had done an "import django.core.template" in there but that did
not work.

NOTE: However, if I do a "from django.core.template import
register_filter" and then modify
the call to register the filter to be:

   register_filter('num_to_gold', num_to_gold, True)

it works. Anyone else working with custom filters and see behaviors
like this?

Reply via email to