Opened 14 years ago

Closed 4 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


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 Russell Keith-Magee, 14 years ago

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 by Luke Plant, 13 years ago

Type: New feature

comment:3 by Luke Plant, 13 years ago

Severity: Normal

comment:4 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 by rameshugar, 7 years ago

Owner: changed from nobody to rameshugar
Status: newassigned

comment:7 by David Smith, 4 years ago

Owner: changed from rameshugar to David Smith

comment:8 by David Smith, 4 years ago

Has patch: set

comment:9 by Claude Paroz, 4 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 Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:11 by David Smith, 4 years ago

Patch needs improvement: unset

comment:12 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:13 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In a350bfa:

Fixed #13009 -- Added BoundField.widget_type property.

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