#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 , 9 years ago
Summary: | add template filter not work with forloop.counter → add template filter doesn't work with mixed types |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
comment:2 by , 9 years ago
Cc: | added |
---|---|
Owner: | changed from | to
Status: | new → assigned |
comment:3 by , 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.
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?
comment:4 by , 9 years ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
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 , 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.
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.