Opened 15 years ago
Closed 5 years ago
#13009 closed New feature (fixed)
provide django.forms field type info for use in templates
Reported by: | Tom von Schwerdtner | Owned by: | David Smith |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | |
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
My use case is that it would be useful to have access to this info from templates when generating forms, eg:
{% for field in form.fields %} {% if field.type == 'checkbox' %} {# render one way... #} {% else %} {# render another way #} {% endif %} {% endfor %}
FWIW, django.contrib.admin seems to get around this problem in AdminField by adding an is_checkbox attribute, but it seems to me that the type of field being rendered should be easily available in the template, in other words, IMHO this makes sense as a core feature of django.forms.
Change History (13)
comment:1 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 14 years ago
Type: | → New feature |
---|
comment:3 by , 14 years ago
Severity: | → Normal |
---|
comment:6 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:7 by , 5 years ago
Owner: | changed from | to
---|
comment:8 by , 5 years ago
Has patch: | set |
---|
comment:9 by , 5 years ago
Repeating here my proposal on the PR: create a new BoundField
property (widget_type
?) returning the lowercased widget class name stripped from the widget
or input
strings?
In that case, if I have defined a MyCustomWidget
, the (bound)field.widget_type would by mycustom
.
comment:10 by , 5 years ago
Patch needs improvement: | set |
---|
comment:11 by , 5 years ago
Patch needs improvement: | unset |
---|
comment:12 by , 5 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Broadly the idea has merit, but it needs a bit of finesse - specifically, what determines the "type" of the field? Do we just use the type of the input element on the widget? What do we do in the case of multi-widgets?
Unfortunately, the solution used in admin isn't a great reference point. Admin has control of all it's widgets, and the way in which they are used. This isn't true of the broader widget framework, which must integrate with arbitrary external widgets, used in unknown ways.