django-utils

personal collection of django utilities
git clone https://git.ce9e.org/django-utils.git

commit
af9add84c1cc3c5d23a07bd1b007b7885f07faf7
parent
559760f97459ee4faf061ff3322ec3b26c0ed020
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2018-10-22 17:21
add datalist widget

Diffstat

A utils/datalist.py 16 ++++++++++++++++
A utils/templates/utils/widgets/datalist.html 6 ++++++

2 files changed, 22 insertions, 0 deletions


diff --git a/utils/datalist.py b/utils/datalist.py

@@ -0,0 +1,16 @@
   -1     1 from django.forms.widgets import TextInput
   -1     2 
   -1     3 
   -1     4 class DatalistWidget(TextInput):
   -1     5     template_name = 'utils/widgets/datalist.html'
   -1     6 
   -1     7     def __init__(self, *args, datalist=[], **kwargs):
   -1     8         self.datalist = datalist
   -1     9         super().__init__(*args, **kwargs)
   -1    10 
   -1    11     def get_context(self, name, value, attrs):
   -1    12         context = super().get_context(name, value, attrs)
   -1    13         attrs = context['widgets']['attrs']
   -1    14         attrs['list'] = attrs['id'] + '_list'
   -1    15         context['datalist'] = self.datalist
   -1    16         return context

diff --git a/utils/templates/utils/widgets/datalist.html b/utils/templates/utils/widgets/datalist.html

@@ -0,0 +1,6 @@
   -1     1 {% include "django/forms/widgets/input.html" %}
   -1     2 <datalist id="{{ widget.attrs.list }}">
   -1     3     {% for option in datalist %}
   -1     4         <option value="{{ option }}">
   -1     5     {% endfor %}
   -1     6 </datalist>