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.
* owner: nobody => shreyas
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28618#comment:1>
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>
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>
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>
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>
* 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>
* owner: shreyas => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/28618#comment:7>