Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a template tag in the interactive shell?

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.

like image 365
nemesisdesign Avatar asked Oct 24 '25 22:10

nemesisdesign


2 Answers

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.

like image 150
dani herrera Avatar answered Oct 27 '25 20:10

dani herrera


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'
like image 45
supervacuo Avatar answered Oct 27 '25 21:10

supervacuo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!