Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#372 closed defect (invalid)

problems loading custom filters?

Reported by: scanner@… Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello, I have a project that has three apps in it right now (and more on the way.) The apps
do build on each other in that the second app requires things in the first and the third app requires
things in the second, etc. Perhaps not a wise idea but it breaks down in to how the model works.

In any case I had a need of a filter that converted numbers like 50000 in to 5g0s0c - 100 c -> 1s 100s -> 1g
I found documentation saying that you could load custom templates and filters but nothing really
saying how to do that.

By looking at code it looks like I create a "templatetags" dir in my app dir (with init.py and all)
and put a file in there that defines the new filter.

Then in my templatetags dir I create a module. This module will contain a function that is the filter
an then contain a line that declares this filter to the rest of the framework:

# Now we have this function to make it truly useful we register it as a filter
# thus it can be used in our html template files directly as <9000>|num_to_gold
#
django.core.template.register_filter('num_to_gold', num_to_gold, True)

Now in the templates that need this filter I put at the top:

{% load items.helpers %}

then I use the filter. When I do this and call it from the dev server (ie: django-admin.py runserver ... )
it works fine.

Now, when I try to call this via the mod python server it fails.
Removing the {% load items.helpers %} from the template and it works.

The failure message is a bit befuddling:

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'

I will upload the file that defines the filter and a template that uses it.

Change History (1)

comment:1 by Jacob, 19 years ago

Resolution: invalid
Status: newclosed

Documentation on custom template tags is forthcomming; for now please ask questions on the django-users mailing list or on IRC. Please don't use the ticket system for support questions; only to report issues with Django itself.

Note: See TracTickets for help on using tickets.
Back to Top