[Django]-How to import a template tag in the interactive shell?

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

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

Leave a comment