from django.template import Template, Context
t = Template(" {{ A.B.attr }}")
output=t.render(Context({"A": A}))
}}}
the output should be "Hello", but it is string_if_invalid, i.e., empty
string "" .
--
Ticket URL: <https://code.djangoproject.com/ticket/29306>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> {{{
> class A:
> class B:
> attr="Hello"
>
> from django.template import Template, Context
> t = Template(" {{ A.B.attr }}")
> output=t.render(Context({"A": A}))
> }}}
>
> the output should be "Hello", but it is string_if_invalid, i.e., empty
> string "" .
New description:
{{{
class A:
def __init__(self, value):
pass
attr="Hello"
from django.template import Template, Context
t = Template(" {{ A.attr }}")
output=t.render(Context({"A": A}))
}}}
the output should be "Hello", but it is string_if_invalid, i.e., empty
string "" .
--
--
Ticket URL: <https://code.djangoproject.com/ticket/29306#comment:1>
* status: new => assigned
* owner: nobody => Luoxzhg
--
Ticket URL: <https://code.djangoproject.com/ticket/29306#comment:2>
* stage: Unreviewed => Accepted
* easy: 1 => 0
Comment:
[https://github.com/django/django/pull/9864 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/29306#comment:3>
Comment (by Tim Graham):
I understand the desire for the behavior change, however, the change could
be backwards incompatible. Consider this case which will pass before the
change but fail afterward:
{{{
@setup({'t': '{{ A.attr }}'})
def test_class_attribute(self):
"""Accessing an attribute of an instantiated class."""
class A:
def __init__(self):
self.attr = 'Hello'
self.assertEqual(self.engine.render_to_string('t', {'A': A}), 'Hello')
}}}
Is there any documentation to suggest
[https://docs.djangoproject.com/en/dev/ref/templates/language/#variables
the current behavior] is incorrect?
--
Ticket URL: <https://code.djangoproject.com/ticket/29306#comment:4>
* status: assigned => closed
* resolution: => duplicate
Comment:
I think this is expected behaviour, that has a workaround.
#15791 added a check for a `do_not_call_in_templates` attribute on a
callable.
Setting this to `True` in the example test case allows it to pass.
This is documented in the
[https://docs.djangoproject.com/en/2.0/ref/templates/api/#variables-and-
lookups Variables and lookups] section of the Template API docs.
Closing as a duplicate of #15791
--
Ticket URL: <https://code.djangoproject.com/ticket/29306#comment:5>