Opened 4 years ago

Closed 4 years ago

#18242 closed New feature (wontfix)

Making widget type accessible in template

Reported by: Selwin Ong Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords:
Cc: Selwin Ong Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi there,

Is there a possibility to add a type attribute to form widgets? The goal is to make it easy for designers to apply different styles to each widget type while keeping the template code DRY.

Django's current source code already does something similar for "<input>" based widgets such as TextInput and DateInput (though for a different purpose). These widgets have input_type attribute attached to them so we can already do:

<ul>
{% for field in form %}
    <li class = '{{ field.field.widget.input_type }}'>
        {{ field }}
    </li>
{% endfor %}
</ul>

But the above means we can only target input based widgets and not select, textarea etc. If we add a more generic type (e.g "select", "checkbox", "textarea") attribute to widgets, we'll be able to do:

<ul>
{% for field in form %}
    <li class = '{{ field.field.widget.type }}'>
        {{ field }}
    </li>
{% endfor %}
</ul>

Or even better (if we attach widgets to BoundField):

<ul>
{% for field in form %}
    <li class = '{{ field.widget.type }}'>
        {{ field }}
    </li>
{% endfor %}
</ul>

If it sounds like a good idea, I'd be more than willing to try writing a patch for this.

Change History (3)

comment:1 Changed 4 years ago by Selwin Ong

Cc: Selwin Ong added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 4 years ago by thomas_scrace

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 4 years ago by Aymeric Augustin

Resolution: wontfix
Status: newclosed

It's already possible to apply arbitrary CSS classes to widgets:
https://docs.djangoproject.com/en/stable/ref/forms/widgets/#django.forms.Widget.attrs

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