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.
* 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>
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>
* cc: gregchapple1@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/22767#comment:3>
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of #18400.
--
Ticket URL: <https://code.djangoproject.com/ticket/22767#comment:4>