[Django] #29306: [bugs for all version]Template variable-lookups system evaluate callables before completed the evaluation

6 views
Skip to first unread message

Django

unread,
Apr 9, 2018, 11:54:10 AM4/9/18
to django-...@googlegroups.com
#29306: [bugs for all version]Template variable-lookups system evaluate callables
before completed the evaluation
-------------------------------------------+--------------------------
Reporter: Luoxzhg | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: master
Severity: Normal | Keywords: template
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------------+--------------------------
{{{
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 "" .

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

Django

unread,
Apr 9, 2018, 8:17:43 PM4/9/18
to django-...@googlegroups.com
#29306: [bugs]Template variable-lookups system invoke class when access class
attribute
---------------------------------+--------------------------------------

Reporter: Luoxzhg | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:

Keywords: template | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by Luoxzhg:

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>

Django

unread,
Apr 9, 2018, 9:17:23 PM4/9/18
to django-...@googlegroups.com
#29306: [bugs]Template variable-lookups system invoke class when access class
attribute
---------------------------------+--------------------------------------
Reporter: Luoxzhg | Owner: Luoxzhg
Type: Bug | Status: assigned

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

Keywords: template | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Luoxzhg):

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


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

Django

unread,
Apr 10, 2018, 11:11:39 AM4/10/18
to django-...@googlegroups.com
#29306: Prevent template variable lookups from intiatiating a class
---------------------------------+------------------------------------

Reporter: Luoxzhg | Owner: Luoxzhg
Type: Bug | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: template | Triage Stage: Accepted

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

* 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>

Django

unread,
Apr 10, 2018, 12:42:42 PM4/10/18
to django-...@googlegroups.com
#29306: Prevent template variable lookups from intiatiating a class
---------------------------------+------------------------------------
Reporter: Luoxzhg | Owner: Luoxzhg
Type: Bug | Status: assigned
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: template | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

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>

Django

unread,
Apr 11, 2018, 4:20:45 AM4/11/18
to django-...@googlegroups.com
#29306: Prevent template variable lookups from intiatiating a class
---------------------------------+-------------------------------------
Reporter: Luoxzhg | Owner: Luoxzhg
Type: Bug | Status: closed

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

Keywords: template | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* 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>

Reply all
Reply to author
Forward
0 new messages