Opened 8 years ago

Closed 5 years ago

#7815 closed (wontfix)

Let Context handle include, extends and inclusion_tag template loading

Reported by: emulbreh Owned by: nobody
Component: Template system Version: master
Severity: Keywords: feature
Cc: goliath.mailinglist@…, research@… Triage Stage: Design decision needed
Has patch: no Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


  • Add a loader kwarg to Context which should be something with a get_template(name) function. Default would be django.template.loader.
  • Provide get_template() and select_template() methods on Context, use these in include, extends and inclusion_tag.
  • Cache templates in Context, optionally: provide a way to populate this cache.
  • Remove ConstantIncludeNode since caching would provide the same benefits and it causes problems with recursive includes.

This would fix

  • #2949 - templating engine too tightly tied to TEMPLATE_DIRS in the settings
  • #3544 - recursive includes
  • #4278 - get_template should accept a dirs argument
  • #6834 - support for templates to be loaded from dynamically-selected directories


class PrefixLoader:
    def __init__(self, prefix_list):
        self.prefix_list = prefix_list
    def get_template(name):
        from django.template.loader import select_template
        return select_template(['%s/%s' % (prefix, name) for prefix in self.prefix_list])

tpl.render(Context({}, loader=PrefixLoader(['a', 'b', 'c'])))

Attachments (1)

7815.tplrf.diff (7.0 KB) - added by emulbreh 8 years ago.
depends on tplrf.2.diff and tplrf-tests.diff from #7806

Download all attachments as: .zip

Change History (9)

Changed 8 years ago by emulbreh

depends on tplrf.2.diff and tplrf-tests.diff from #7806

comment:1 Changed 8 years ago by emulbreh

  • Needs documentation set
  • Needs tests set
  • Patch needs improvement unset
patch -p0 tplrf.2.diff
patch -p0 tplrf-tests.diff
patch -p1 7815.tplrf.diff

mostly done. contains a test for recursive includes, but no tests for the loader feature yet.

comment:2 Changed 8 years ago by David Danier <goliath.mailinglist@…>

  • Cc goliath.mailinglist@… added

comment:3 Changed 8 years ago by emulbreh

This would also fix #7931.

Forgot to mention in the ticket description: this is a backwards incompatible change. If you rely on compile time side effects (e.g. {% load %}) in included templates, that will break.

comment:4 Changed 8 years ago by ericholscher

  • milestone set to 1.0
  • Triage Stage changed from Unreviewed to Design decision needed

comment:5 Changed 8 years ago by jacob

  • milestone changed from 1.0 to post-1.0

comment:6 Changed 7 years ago by anonymous

  • Cc research@… added

comment:7 Changed 7 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:8 Changed 5 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

The three (remaining) individual tickets (#3544, #4278, #6834) are all good ideas, but an omnibus patch makes it really hard to evaluate which bits are fixing what. It's also an area that needs a lot of cleanup following the 1.2 template loader changes.

Closing wontfix, mostly as a procedural thing; we'd prefer to see single patches for single issues, or a merging of issues into a single ticket (resulting in the previous tickets getting closed).

Note: See TracTickets for help on using tickets.
Back to Top