[Django] #22767: Unexpected value for an unset variable with length filter in templates

30 views
Skip to first unread message

Django

unread,
Jun 4, 2014, 6:31:48 PM6/4/14
to django-...@googlegroups.com
#22767: Unexpected value for an unset variable with length filter in templates
---------------------------------+--------------------
Reporter: Walkman | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
Not sure if this is a bug or I'm just missing something, but I expected
this to be `False` anyway:
`{% if groups|length > 0 %}`

This will be `True` in Django 1.6.5 if `groups` is an unset variable. The
`TEMPLATE_STRING_IF_INVALID` setting has the default value (empty string).

I know it's not idiomatic code and I fixed it:
`{% if groups %}`
(which works as expected), but other newbies like me can have a hard time
figuring out what's wrong.

I checked `{% if None|length > 0 %}` and it is also `True`, which is
clearly not expected.

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

Django

unread,
Jun 5, 2014, 8:45:16 AM6/5/14
to django-...@googlegroups.com
#22767: Unexpected value for an unset variable with length filter in templates
---------------------------------+--------------------------------------

Reporter: Walkman | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

I agree that this is somewhat unexpected, though I don't know if it's a
bug. If you were to do the same in Python, and tried to get the length of
something which is None, you would get a TypeError. To get around that you
would either use a try/except block, or first check if the variable is not
None. I think it's reasonable to do the same in your templates. ie:

{{{
{% if groups %}


{% if groups|length > 0 %}

groups!
{% else %}
no groups :(
{% endif %}
{% else %}
groups not defined
{% end %}
}}}

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

Django

unread,
Jun 5, 2014, 9:02:55 AM6/5/14
to django-...@googlegroups.com
#22767: Unexpected value for an unset variable with length filter in templates
---------------------------------+--------------------------------------

Reporter: Walkman | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by gchp):

Saying that, I would still expect the `length` filter to return `0` unless
the length is indeed something other than that. From taking a quick look,
it seems to return an empty string if the value given does not support
`len()`. I changed this to instead return `0` and it does indeed fix the
issue, though it makes a ton of tests fail. Perhaps someone with a little
more experience with filters can provide some insight as to why it
currently returns a string?

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

Django

unread,
Jun 5, 2014, 9:03:09 AM6/5/14
to django-...@googlegroups.com
#22767: Unexpected value for an unset variable with length filter in templates
---------------------------------+--------------------------------------

Reporter: Walkman | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* cc: gregchapple1@… (added)


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

Django

unread,
Jun 5, 2014, 9:08:28 AM6/5/14
to django-...@googlegroups.com
#22767: Unexpected value for an unset variable with length filter in templates
---------------------------------+--------------------------------------
Reporter: Walkman | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.6
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #18400.

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

Reply all
Reply to author
Forward
0 new messages