Login

Load templatetag libraries via settings

Author:
miracle2k
Posted:
July 26, 2007
Language:
Python
Version:
.96
Tags:
templates settings tags templatetags conf
Score:
7 (after 7 ratings)

In your settings file:

TEMPLATE_TAGS = ( "djutils.templatetags.sqldebug", )

Make sure load_templatetags() gets called somewhere, for example in your apps init.py

Edit: Updated to work with templatetag libraries that use certain critical django-bits.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def load_templatetags():
    from django.conf import settings
    from django.template import add_to_builtins
    # This is important: If the function is called early, and some of the custom
    # template tags use superclasses of django template tags, or otherwise cause
    # the following situation to happen, it is possible that circular imports 
    # cause problems: 
    # If any of those superclasses import django.template.loader (for example,
    # django.template.loader_tags does this), it will immediately try to register
    # some builtins, possibly including some of the superclasses the custom template
    # uses. This will then fail because the importing of the modules that contain 
    # those classes is already in progress (but not yet complete), which means that 
    # usually the module's register object does not yet exist.
    # In other words:
    #       {custom-templatetag-module} ->
    #       {django-templatetag-module} ->
    #       django.template.loader ->
    #           add_to_builtins(django-templatetag-module)
    #           <-- django-templatetag-module.register does not yet exist
    # It is therefor imperative that django.template.loader gets imported *before*
    # any of the templatetags it registers.
    import django.template.loader

    try:
        for lib in settings.TEMPLATE_TAGS:
            add_to_builtins(lib)
    except AttributeError:
        pass

More like this

  1. Support for {% macro %} tags in templates, version 2 by mludvig 7 years, 11 months ago
  2. LinkShare Pixel Tracking template tag for Satchmo by maxk 4 years, 6 months ago
  3. Sorl thumbs for the lazy by renyi 4 years, 2 months ago
  4. Simple Plone Migration by msm-art 7 years, 6 months ago
  5. Mobile browser detection middleware by pavl 5 years, 3 months ago

Comments

steve_cassidy51 (on December 11, 2007):

This worked for me until I updated my django (svn) when I started getting a crash on startup due to unset ENV variables. I had the code in the main --init--.py (how do you do underscores?) in my project. Moving it to --init--.py in a subdirectory (application) removed the message and made everything happy again.

#

rbell01824 (on April 5, 2014):

There is a small bug in the import. It should read:

from django.template.loader import add_to_builtins

Note additionally that the template tag should be registered as:

<application>.templatetags.<template tag lib>

#

Please login first before commenting.