Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#25118 closed Cleanup/optimization (wontfix)

add template filter doesn't work with mixed types

Reported by: Daniel Owned by: Konrad Świat
Component: Template system Version: 1.8
Severity: Normal Keywords:
Cc: konrad.swiat@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

{% with identifier="#id"|add:forloop.counter %}
{{  identifier }}
{% endwith %}

print empty value

Change History (5)

comment:1 by Tim Graham, 9 years ago

Summary: add template filter not work with forloop.counteradd template filter doesn't work with mixed types
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

The issue is that the add filter doesn't support mixed types (string and integer in this case). It might be reasonable to add such support.

comment:2 by Konrad Świat, 9 years ago

Cc: konrad.swiat@… added
Owner: changed from nobody to Konrad Świat
Status: newassigned

comment:3 by Konrad Świat, 9 years ago

https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#add

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.

References: #11687, #16676

The question is, how the add filter should behave. If adding fails, do we expect the result to be concatenated string? Maybe we need a separate filter to do that?

Last edited 9 years ago by Konrad Świat (previous) (diff)

comment:4 by Tim Graham, 9 years ago

Resolution: wontfix
Status: assignedclosed

I guess it's a "won't fix" based on the discussion in #16676 then. I don't think Django needs to add a new filter for this case.

comment:5 by Chad Stovern, 9 years ago

This seems to not be a bug. I did some simple tests to confirm the behavior is consistent with the documentation.

The add filter correctly concats strings.
It also correctly coerces a string to an int adding allowing it to be added to a subsequent int.
Two ints are also correctly added.
Lastly a string like my name fails to be coerced to an int and when added to an int returns an empty value.

Once again, this is consistent with the docs. Thanks.

Note: See TracTickets for help on using tickets.
Back to Top