Code

Opened 2 years ago

Closed 13 months ago

#18242 closed New feature (wontfix)

Making widget type accessible in template

Reported by: selwin Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords:
Cc: selwin 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.

Attachments (0)

Change History (3)

comment:1 Changed 2 years ago by selwin

  • Cc selwin added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by thomas_scrace

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 13 months ago by aaugustin

  • Resolution set to wontfix
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.