Login

Load templatetag libraries via settings

Author:
miracle2k
Posted:
July 26, 2007
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 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.