Load templatetag libraries via settings

 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 to clear cached template fragment by joao.coelho 3 years, 6 months ago
  2. LinkShare Pixel Tracking template tag for Satchmo by maxk 2 years, 4 months ago
  3. Paginator TemplateTag by trbs 5 years, 1 month ago
  4. Automatically trim newforms text fields by miracle2k 5 years, 8 months ago
  5. email_links by sansmojo 5 years, 11 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.

#

(Forgotten your password?)