[Django] #20995: {% include %} uses get_template where it could select_template

17 views
Skip to first unread message

Django

unread,
Aug 29, 2013, 10:18:08 AM8/29/13
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-----------------------------------------+--------------------
Reporter: Keryn Knight <django@…> | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------------+--------------------
It'd be nice if the Include template tag was sensible enough to allow
fallbacks by selecting the most appropriate template, as things like
`render`/`render_to_response`/`render_to_string` do. It's tripped me up on
more than one occasion, and it seems a trivial feature to support, from my
limited testing.
{{{
>>> from django.template import Template, Context
>>> tmpl = Template('{% include var %}')
>>> ctx = Context({'var':'admin/base.html'})
>>> ctx
[{'var': 'admin/base.html'}]
>>> tmpl.render(ctx)
... some HTML output ...
>>> ctx.update({'var':['admin/base.html', 'admin/fail.html']})
{'var': ['admin/base.html', 'admin/fail.html']}
>>> tmpl.render(ctx)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/path/django/template/base.py", line 140, in render
return self._render(context)
File "/path/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/path/django/template/base.py", line 823, in render
bit = self.render_node(node, context)
File "/path/django/template/debug.py", line 74, in render_node
return node.render(context)
File "/path/django/template/loader_tags.py", line 165, in render
template = get_template(template_name)
File "/path/django/template/loader.py", line 145, in get_template
template, origin = find_template(template_name)
File "/path/django/template/loader.py", line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: ['admin/base.html', 'admin/fail.html']
}}}

The 'fix' is to change
[https://github.com/django/django/blob/5cdacbda034af928f5033c9afc7b50ee0b13f75c/django/template/loader_tags.py#L166
this line] from `get_template` to `select_template`, though this might now
be slightly complicated by the recent changes in
5cdacbda034af928f5033c9afc7b50ee0b13f75c to allow for rendering of
`Template` instances.

Changing to `select_template` on 1.4 yields the results I'd expect:
{{{
>>> from django.template import Template, Context
>>> tmpl = Template('{% include var %}')
>>> ctx = Context({'var':['admin/base.html', 'admin/fail.html']})
>>> tmpl.render(ctx)
... some HTML output ...
}}}

Both the above shell sessions assume `django.contrib.admin` is in
INSTALLED_APPS.

--
Ticket URL: <https://code.djangoproject.com/ticket/20995>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 6, 2013, 12:36:45 PM9/6/13
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | amberdoctor
Type: New feature | Status: assigned

Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 1 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by amberdoctor):

* owner: nobody => amberdoctor
* status: new => assigned
* needs_docs: => 0
* needs_tests: => 0
* needs_better_patch: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:1>

Django

unread,
Sep 7, 2013, 12:39:14 PM9/7/13
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | amberdoctor
Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 1 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Keryn Knight <django@…>):

I now have a working patch + test for this, but because it introduces
another slightly different way of selecting a template based on the given
arguments, I'm holding off on opening a PR, to see if there's merit to
refactoring template selection into one place; see #21065 for the details.

--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:2>

Django

unread,
Sep 13, 2013, 5:30:28 PM9/13/13
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | amberdoctor
Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by anonymous):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:3>

Django

unread,
Sep 13, 2013, 5:33:31 PM9/13/13
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | amberdoctor
Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by EvilDMP):

* stage: Unreviewed => Accepted

Django

unread,
Sep 13, 2013, 5:36:44 PM9/13/13
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | amberdoctor
Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by EvilDMP):

I agree that this should follow upon a resolution of #21065. Three
different ways of selecting templates seems like two too many already.

--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:4>

Django

unread,
Sep 19, 2013, 9:49:35 AM9/19/13
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | amberdoctor
Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* easy: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:5>

Django

unread,
Feb 2, 2020, 11:57:00 AM2/2/20
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | amberdoctor
Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by KESHAV KUMAR):

* needs_docs: 0 => 1
* has_patch: 0 => 1


Comment:

Tried to solve this. [https://github.com/django/django/pull/12407 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:6>

Django

unread,
Feb 12, 2020, 6:01:37 PM2/12/20
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | amberdoctor
Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam (Chainz) Johnson):

* needs_better_patch: 0 => 1
* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:7>

Django

unread,
Feb 12, 2020, 6:03:35 PM2/12/20
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: KESHAV
<django@…> | KUMAR

Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam (Chainz) Johnson):

* owner: amberdoctor => KESHAV KUMAR


--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:8>

Django

unread,
Feb 13, 2020, 10:36:39 PM2/13/20
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: KESHAV
<django@…> | KUMAR
Type: New feature | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by KESHAV KUMAR):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:9>

Django

unread,
Feb 18, 2020, 1:16:13 AM2/18/20
to django-...@googlegroups.com
#20995: {% include %} uses get_template where it could select_template
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: KESHAV
<django@…> | KUMAR
Type: New feature | Status: closed

Component: Template system | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"f37d548ede290690589e86b892c4f106e2a8e1bc" f37d548]:
{{{
#!CommitTicketReference repository=""
revision="f37d548ede290690589e86b892c4f106e2a8e1bc"
Fixed #20995 -- Added support for iterables of template names to {%
include %} template tag.

Thanks Adam Johnson for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/20995#comment:10>

Reply all
Reply to author
Forward
0 new messages