[Django] #28618: There should be a way to make Templates substitution to raise an exception on error

38 views
Skip to first unread message

Django

unread,
Sep 19, 2017, 9:31:26 AM9/19/17
to django-...@googlegroups.com
#28618: There should be a way to make Templates substitution to raise an exception
on error
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
facundobatista |
Type: New | Status: new
feature |
Component: Template | Version: 1.11
system | Keywords: template error
Severity: Normal | substitution
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
My idea is to have a way (maybe a config flag, default to False) to make
templates subsystem to raise an error in the case of a substitution
problem (and not ignore silently or fill with a default value).

My goal here is to enable this flag *when running my project tests*, not
in production.

Thanks!

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

Django

unread,
Sep 20, 2017, 12:25:38 PM9/20/17
to django-...@googlegroups.com
#28618: There should be a way to make Templates substitution to raise an exception
on error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: shreyas
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage:
substitution | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* owner: nobody => shreyas
* status: new => assigned


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

Django

unread,
Sep 20, 2017, 12:34:12 PM9/20/17
to django-...@googlegroups.com
#28618: There should be a way to make Templates substitution to raise an exception
on error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: shreyas
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage:
substitution | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by shreyas):

It seems that there is a way to pass parameter `string_if_invalid` when
initializing template engine which will be rendered in template if
variable is not resolved from context. You can also pass `%s` in that
string which will be interpolated with missing variable name.

For Eg.

{{{

engine = Engine(debug=True, string_if_invalid="Variable not found %s")
t = engine.from_string("{{articlecovfefe.section}}")
c = Context({"article": {"section": "News"}})
print(t.render(c)) # prints 'Variable not found articlecovfefe.section'
!= 'News'

}}}


Is it possible to leverage this in test cases for you.

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

Django

unread,
Sep 20, 2017, 3:32:11 PM9/20/17
to django-...@googlegroups.com
#28618: There should be a way to make Templates substitution to raise an exception
on error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: shreyas
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage:
substitution | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Facundo Batista):

Yes, a string can be fixed so if a substitution fails, that can be
searched in the test.

But that would imply to remember on EVERY test to check if that string
exists (or change your testing infrastructure to assert that on all
tests...).

In the spirit of "Errors should never pass silently" I think it would be
useful to have a parameter like Engine(...,
exception_on_missing_substitution=True) with default to False, so any
missing substitution would explicitly make the test fail, without the
developer needing to remember anything...

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

Django

unread,
Sep 21, 2017, 2:13:44 AM9/21/17
to django-...@googlegroups.com
#28618: There should be a way to make Templates substitution to raise an exception
on error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: shreyas
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage:
substitution | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by shreyas):

Good point.

Let's discuss this in Django dev mailing list and get other people's
opinion.

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

Django

unread,
Sep 26, 2017, 8:52:35 PM9/26/17
to django-...@googlegroups.com
#28618: There should be a way to make Templates substitution to raise an exception
on error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: shreyas
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage:
substitution | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

[https://groups.google.com/d/topic/django-
developers/ZMzF0PgNb-k/discussion django-developers discussion]

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

Django

unread,
Sep 28, 2017, 2:26:49 PM9/28/17
to django-...@googlegroups.com
#28618: Add an easier to make templates substitutions raise an exception on error
-------------------------------------+-------------------------------------

Reporter: Facundo Batista | Owner: shreyas
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 0 | Needs documentation: 0

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

* stage: Unreviewed => Accepted


Comment:

As noted on the mailing list, this is already possible by setting
`TEMPLATES ['OPTIONS']['string_if_invalid']` to a class with a `__str__()`
that raises an exception. A proposal from the mailing list thread is to
add a new `'if_invalid'` option that's a callable which would allow more
advanced behavior (raise an exception, logging, etc.). Tentatively
accepting that approach. See #24199 for some related information about the
problem.

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

Django

unread,
Oct 28, 2021, 5:13:15 AM10/28/21
to django-...@googlegroups.com
#28618: Add an easier to make templates substitutions raise an exception on error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: (none)
Type: New feature | Status: new

Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 0 | Needs documentation: 0

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

* owner: shreyas => (none)
* status: assigned => new


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

Django

unread,
Mar 10, 2025, 9:31:40 PM3/10/25
to django-...@googlegroups.com
#28618: Add an easier to make templates substitutions raise an exception on error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: Ahmed
| Nassar
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ahmed Nassar):

* owner: (none) => Ahmed Nassar
* status: new => assigned

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

Django

unread,
Mar 11, 2025, 10:38:34 AM3/11/25
to django-...@googlegroups.com
#28618: Add an easier way to make templates substitutions raise an exception on
error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: Ahmed
| Nassar
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* summary:
Add an easier to make templates substitutions raise an exception on
error
=>
Add an easier way to make templates substitutions raise an exception
on error

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

Django

unread,
Apr 7, 2025, 4:52:23 AM4/7/25
to django-...@googlegroups.com
#28618: Add an easier way to make templates substitutions raise an exception on
error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: Ahmed
| Nassar
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ahmed Nassar):

* has_patch: 0 => 1

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

Django

unread,
Apr 7, 2025, 6:08:43 AM4/7/25
to django-...@googlegroups.com
#28618: Add an easier way to make templates substitutions raise an exception on
error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: Ahmed
| Nassar
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/28618#comment:11>

Django

unread,
Apr 8, 2025, 12:58:59 AM4/8/25
to django-...@googlegroups.com
#28618: Add an easier way to make templates substitutions raise an exception on
error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: Ahmed
| Nassar
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ahmed Nassar):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/28618#comment:12>

Django

unread,
Apr 8, 2025, 2:57:36 PM4/8/25
to django-...@googlegroups.com
#28618: Add an easier way to make templates substitutions raise an exception on
error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: Ahmed
| Nassar
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Carlton Gibson):

The current PR is only adding an option to raise the VariableNotFound
exception, and only as a “global” flag on the template backend.

Given how prevalent the reliance on missing variables is (and missing
steps in variable chains) I’m not this will be at all usable. (There was
some discussion recently about allowing such opt-in to be per template,
for example. [https://forum.djangoproject.com/t/raise-error-for-missing-
variable-used-in-template/39776/9 forum thread] )

There’s nothing in the PR addressing Tim’s points from comment:6, which
was the basis for accepting.
--
Ticket URL: <https://code.djangoproject.com/ticket/28618#comment:13>

Django

unread,
Apr 14, 2025, 4:06:40 AM4/14/25
to django-...@googlegroups.com
#28618: Add an easier way to make templates substitutions raise an exception on
error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: Ahmed
| Nassar
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/28618#comment:14>

Django

unread,
Apr 18, 2025, 11:21:03 AM4/18/25
to django-...@googlegroups.com
#28618: Add an easier way to make templates substitutions raise an exception on
error
-------------------------------------+-------------------------------------
Reporter: Facundo Batista | Owner: Ahmed
| Nassar
Type: New feature | Status: assigned
Component: Template system | Version: 1.11
Severity: Normal | Resolution:
Keywords: template error | Triage Stage: Accepted
substitution |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Dan LaManna):

* cc: Dan LaManna (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/28618#comment:15>
Reply all
Reply to author
Forward
0 new messages