Opened 8 years ago

Closed 6 years ago

#7815 closed (wontfix)

Let Context handle include, extends and inclusion_tag template loading

Reported by: Johannes Dollinger 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 Johannes Dollinger 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 Johannes Dollinger

Attachment: 7815.tplrf.diff added

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

comment:1 Changed 8 years ago by Johannes Dollinger

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 Johannes Dollinger

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 Eric Holscher

milestone: 1.0
Triage Stage: UnreviewedDesign decision needed

comment:5 Changed 8 years ago by Jacob

milestone: 1.0post-1.0

comment:6 Changed 8 years ago by anonymous

Cc: research@… added

comment:7 Changed 8 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:8 Changed 6 years ago by Russell Keith-Magee

Resolution: wontfix
Status: newclosed

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