Search code examples
djangodjango-templatesdjango-custom-tags

How can I load custom Django tags from a file?


I have a tag definition for mytag in a mytag.py file. This works fine when I'm using a Django project with a settings.py and INSTALLED_APPS -- I append 'myapp' to the list and place mytag.py in myapp/templatetags.

Now I'm using django.conf.settings.configure and I don't have a module with a templatetags subdirectory -- how can I load mytag.py?


Update:

I now tried to use a module with a templatetags directory, but I can't get it to work. Here's my setup:

Files:

  • myapp
    • __init.py__
    • templatetags
      • __init__.py
      • mytag.py
  • program
    • test.py
    • test.html

This is the relevant code:

# test.py

from django.template import Template, Context
from django.conf import settings

from mostlystatic.processors.mostlystaticprocessor import MostlystaticProcessor
from mostlystatic.stuff import DebugLogger

import sys
sys.path.append("~/")

settings.configure(
    DEBUG=True,
    TEMPLATE_DEBUG = True,
    INSTALLED_APPS = ("myapp",)
    )

t = Template(open("test.html").read())
c = Context({})
content = t.render(c)

print content

# test.html

{% load mytag %}

# mytag.py (doesn't load)

from classytags.arguments import Argument
from classytags.core import Tag, Options
from django import template

register = template.Library()

class MyTag(Tag):
    name="mytag"

    def render_tag(self, context:
        return "test"

register.tag(MyTag)

When I run test.py I get this message:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    t = Template(open("test.html").read())
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 125, in __init__
    self.nodelist = compile_string(template_string, origin)
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 153, in compile_string
    return parser.parse()
  File "/Library/Python/2.7/site-packages/django/template/base.py", line 270, in parse
    compiled_result = compile_func(self, token)
  File "/Library/Python/2.7/site-packages/django/template/defaulttags.py", line 1033, in load
    (taglib, e))
django.template.base.TemplateSyntaxError: 'mytag' is not a valid tag library: Template library mytag not found, tried django.templatetags.mytag

Solution

  • The easiest thing to do is make a module with a templatetags subdirectory, and include that module in INSTALLED_APPS when you configure the settings.

    Any other approach is going to involve wrestling with the Django internals.

    If your script doesn't work, try stripping it down to something really simple like below, then build it back up.

    from django.conf import settings
    settings.configure(INSTALLED_APPS=('my_app',))
    
    from django.template import Template
    # my_tags does not exist, but the error shows that it is 
    # searching my_app's template tag directory
    Template.render("{% load my_tags %}")
    TemplateSyntaxError: 'my_tags' is not a valid tag library: Template library my_tags not found, tried django.templatetags.my_tags, my_app.templatetags.my_tags