#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
 Keywords:             |   Triage Stage:  Unreviewed
Has patch:  0          |  Easy pickings:  0
    UI/UX:  0          |
-----------------------+---------------------------------
 The 1.3 documentation for the `add` filter states:

 > This filter will first try to coerce both values to integers. If this
 fails, it'll attempt to add the values together anyway. This will work on
 some data types (strings, list, etc.) and fail on others. If it fails, the
 result will be an empty string.

 When the value and argument represent the second case ("If this fails,
 it'll attempt to add the values together anyway.") and one is a string
 while the other is a number, the result of the `add` filter is simply the
 value.

 Similar to the fix for ticket #393, I think that for this second case and
 if either value or argument to the filter is a string, the other value
 should be stringified.

 === Test cases ===

 {{{
 {{ 'test'|add:2 }}
 }}}

 Result is: `test` \\
 Expected result: `test2`

 {{{
 {{ 2|add:'test' }}
 }}}

 Result is: `2` \\
 Expected result: `2test`

 === Workarounds ===
 For the first test case, one workaround is to use a `{% with %}` tag:

 {{{
 {% with 2|stringformat:'d' as arg_as_str %}
 {{ 'test'|add:arg_as_str }}
 {% endwith %}
 }}}

 For the second test case, the `stringformat` filter can be used to convert
 the number to a string before `add` is applied:
 {{{
 {{ 2|stringformat:'d'|add:'test' }}
 }}}

 === Related tickets ===
 * Ticket #393 [patch] Filters don't take the str() value of a var
 * Ticket #8088 Template system improvement: "cat" filter, "include" tag
 with filters
 * Ticket #11687 The 'add' template filter only works for integers, and can
 fail noisily

-- 
Ticket URL: <https://code.djangoproject.com/ticket/16676>
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.

Reply via email to