#32125: Added basic support for <datalist> elements for suggestions in Input
widgets
-------------------------------------+-------------------------------------
               Reporter:  Volodymyr  |          Owner:  nobody
                   Type:  New        |         Status:  new
  feature                            |
              Component:  Forms      |        Version:  master
               Severity:  Normal     |       Keywords:  datalist, forms,
           Triage Stage:             |  html5
  Unreviewed                         |      Has patch:  0
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  1          |
-------------------------------------+-------------------------------------
 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'''
 {{{#!python
 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'''
 {{{#!python
 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
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32125>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.53cf9e75f234d99c1b00089331e40cc9%40djangoproject.com.

Reply via email to