Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#28502 closed Bug (fixed)

Select widget doesn't accept non-string as value

Reported by: Nguyễn Hồng Quân Owned by: Srinivas Reddy Thatiparthy
Component: Template system Version: master
Severity: Normal Keywords: widget filter
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I made a model, with age_bracket field, which accepts values from choices like this:

AGE_BRACKET = (
    # The value in the first column will be saved in Postgres Range field,
    # so the upper boundary is not counted, i.e for (18, 25) range, the value 25
    # is not counted.
    ((None, 18), _('Under 18')),
    ((18, 25), _('18 - 24')),
    ((25, 36), _('25 - 35')),
    ((36, 46), _('36 - 45')),
    ((46, 56), _('46 - 55')),
    ((46, 66), _('56 - 65')),
    ((66, None), _('Above 65')),
)

However, when being rendered as form, all values become empty string. This happens since Django 1.11. In Django 1.10, it was OK.
I found the cause is the widget template select_option.html

<option value="{{ widget.value|stringformat:'s' }}"{% include "django/forms/widgets/attrs.html" %}>{{ widget.label }}</option>

The stringformat filter turns every non-string value to empty string.
My suggestion is that, update stringformat so that it returns str(value) if value is not a string. Is it right way to do?

Change History (10)

comment:1 Changed 3 years ago by Claude Paroz

Component: FormsTemplate system
Triage Stage: UnreviewedAccepted
Version: 1.11master

I also think that stringformat should be improved to accept tuples as its base value. In Python the printf syntax special cases tuples as it considers them as a list of placeholder values. This does not make sense in stringformat as the string containing the placeholder is always a single placeholder.

comment:2 Changed 3 years ago by Srinivas Reddy Thatiparthy

Owner: changed from nobody to Srinivas Reddy Thatiparthy
Status: newassigned

comment:3 Changed 3 years ago by Srinivas Reddy Thatiparthy

comment:4 Changed 3 years ago by Claude Paroz

Has patch: set
Needs tests: set

comment:5 Changed 3 years ago by Srinivas Reddy Thatiparthy

Needs tests: unset

comment:6 Changed 3 years ago by Claude Paroz <claude@…>

In a64f88f:

Refs #28502 -- Completed stringformat filter tests

comment:7 Changed 3 years ago by felixxm

Triage Stage: AcceptedReady for checkin

comment:8 Changed 3 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: assignedclosed

In 4ead705c:

Fixed #28502 -- Made stringformat template filter accept tuples

comment:9 Changed 3 years ago by Tim Graham <timograham@…>

In ed77bea:

Refs #28502 -- Complemented stringformat tuple handling/test.

An additional test and a code change were suggested in a late review.

comment:10 Changed 3 years ago by Tim Graham <timograham@…>

In dd82f1df:

[1.11.x] Fixed #28502 -- Made stringformat template filter accept tuples.

Backport of 4ead705cb3cf04bb7551ac037d1e11f682b62bcf and
ed77bea58274e11e5a9e4c8b9650f50deb8a2b26 from master

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