#16676: The 'add' filter should stringify value or argument if the other value
is a
string
-------------------------------------+---------------------------------
Reporter: dtrebbien | Owner: nobody
Type: Bug | Status: new
Milestone: | Component: Template system
Version: 1.3 | Severity: Normal
Resolution: | Keywords:
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+---------------------------------
Changes (by aaugustin):
* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0
Comment:
In Python it's an error to add a string and an int:
{{{
>>> 'a' + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> 1 + 'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
}}}
In this case, the proper output is an empty string, as the docs says. I
could confirm that Django returns the first value. It's a bug.
----
However, `|add` doesn't really make sense with strings. `+` on strings is
just concatenation, and concatenation is natively provided by the template
engine.
Instead of `{{ strval|add:intval }}`, just write `{{ strval }}{{ intval
}}`.
For this reason, I don't like the idea of converting both values to
strings.
----
I'm attaching a patch with tests (actually, the tests existed, but they
contradicted the documentation; I think the documentation has precedence).
I also fixed a naked except -- we never want to catch !BaseException.
--
Ticket URL: <https://code.djangoproject.com/ticket/16676#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.