Opened 4 years ago
Last modified 19 months ago
#32125 closed New feature
Added basic support for <datalist> elements for suggestions in Input widgets — at Initial Version
Reported by: | Volodymyr | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | datalist, forms, html5 |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | yes |
Description
HTML5 introduces a support for <datalist>
HTML5 elements for suggestions in input tag
To do this in Django currently, you have to do something like:
django/forms/widgets/datalist.html
{% include "django/forms/widgets/input.html" %} <datalist id="{{ widget.id }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% for group_name, group_choices, group_index in widget.optgroups %}{% if group_name %} <optgroup label="{{ group_name }}">{% endif %}{% for option in group_choices %} {% include option.template_name with widget=option %}{% endfor %}{% if group_name %} </optgroup>{% endif %}{% endfor %} </datalist>
django/forms/widgets/datalist_option.html
<option value="{{ widget.value|stringformat:'s' }}"{% include "django/forms/widgets/attrs.html" %}>
django/forms/widgets.py
class Datalist(Select): input_type = 'text' template_name = 'django/forms/widgets/datalist.html' option_template_name = 'django/forms/widgets/datalist_option.html' add_id_index = False checked_attribute = {'selected': True} option_inherits_attrs = False
django/forms/fields.py
class DatalistField(ChoiceField): widget = Datalist default_error_messages = { 'invalid_choice': _('Select a valid choice. %(value)s is not one of the available choices.'), } def __init__(self, *, choices='', **kwargs): super().__init__(**kwargs) self.choices = choices
Note:
See TracTickets
for help on using tickets.