3

How can I import a custom template tag or filter in the interactive shell to see if the everything is working fine?

I have two machines behaving differently and I don't have a clue of how to do some debugging.

On the production machine I can't load a template filter, I get the error "Template library not found". On the local machine everything works fine.

nemesisdesign
  • 8,159
  • 12
  • 58
  • 97

2 Answers2

6

Importing filters like this:

from django.template import defaultfilters as filters
filters.date( date.today() )

Instead default filters you should import your custom filter:

from myApp.templatetags import poll_extras
poll_extras.cut( 'ello' )

Double check settings installed app in your production server.

dani herrera
  • 48,760
  • 8
  • 117
  • 177
3

If you're worried about typos, missing __init__.py problems or masked ImportErrors, you could just import the function. Assuming the following structure:

foo
├── bar
│   ├── __init__.py
│   ├── models.py
│   ├── static
│   │   └── ..
│   ├── templates
│   │   └── ..
│   ├── templatetags
│   │   ├── __init__.py
│   │   └── baz.py
│   ├── views.py
├── manage.py
└── foo
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

and the following contents of baz.py:

from django import template

register = template.Library()

@register.filter
def capitalize(value):
    return value.capitalize()

you would just run

>>> from bar.templatetags import baz
>>> print baz.capitalize('test')
'test'
supervacuo
  • 9,072
  • 2
  • 44
  • 61