Opened 14 years ago

Closed 3 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 Changed 14 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

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.

comment:2 Changed 13 years ago by Luke Plant

Type: New feature

comment:3 Changed 13 years ago by Luke Plant

Severity: Normal

comment:4 Changed 12 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 Changed 12 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 Changed 6 years ago by rameshugar

Owner: changed from nobody to rameshugar
Status: newassigned

comment:7 Changed 4 years ago by David Smith

Owner: changed from rameshugar to David Smith

comment:8 Changed 4 years ago by David Smith

Has patch: set

comment:9 Changed 4 years ago by Claude Paroz

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 Changed 3 years ago by Mariusz Felisiak

Patch needs improvement: set

comment:11 Changed 3 years ago by David Smith

Patch needs improvement: unset

comment:12 Changed 3 years ago by Mariusz Felisiak

Triage Stage: AcceptedReady for checkin

comment:13 Changed 3 years ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In a350bfa:

Fixed #13009 -- Added BoundField.widget_type property.

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