Django: Create custom template tag -> ImportError

Posted by Alexander Scholz on Stack Overflow See other posts from Stack Overflow or by Alexander Scholz
Published on 2013-10-20T15:41:30Z Indexed on 2013/10/20 15:54 UTC
Read the original article Hit count: 199

Filed under:
|
|

I'm sorry to ask this again, but I tried several solutions from stack overflow and some tutorials and I couldn't create a custom template tag yet. All I get is ImportError: No module named test_tag when I try to start the server via python manage.py runserver.

I created a very basic template tag (found here: django templatetag?) like so:

My folder structure:

demo
    manage.py
    test
        __init__.py
        settings.py
        urls.py
        ...
        templatetags
            __init__.py
            test_tag.py

test_tag.py:

from django import template
register = template.Library()

@register.simple_tag
def test_tag(input):
    if "foo" == input:
        return "foo"
    if "bar" == input:
        return "bar"
    if "baz" == input:
        return "baz"
    return ""

index.html:

{% load test_tag %}
<html>
    <head>
    ...
    </head>
    <body>
        {% cms_toolbar %}

        {% foobarbaz "bar" %}
        {% foobarbaz "elephant" %}
        {% foobarbaz "foo" %}
    </body>
</html>

and my settings.py:

INSTALLED_APPS = (
    ...
    'test_tag',
    ...
)

Please let me know if you need further information from my settings.py and what I did wrong so I can't even start my server. (If I delete 'test_tag' from installed apps I can start the server but I get the error that test_tag is not known, of course).

Thanks

© Stack Overflow or respective owner

Related posts about python

Related posts about django