Split a string to a list and add to select options

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django import template
from django.utils.safestring import mark_safe, SafeData
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def split_as_option(value, splitter='|', autoescape=None):
    if not isinstance(value, SafeData):
        value = mark_safe(value)
    value = value.split(splitter)
    result = ""
    for v in value:
        result += '<option value="%s">%s</option>\n' % (v, v)
    return mark_safe(result)
split_as_option.is_safe = True
split_as_option.needs_autoescape = True

More like this

  1. split filter by Ciantic 6 years ago
  2. Type checking templatetag filters by marcorogers 3 years, 2 months ago
  3. Allow disabling options in a select widget by scjody 1 year, 11 months ago
  4. SelectDateWidget by silent1mezzo 2 years, 5 months ago
  5. Pagination/Filtering Alphabetically by zain 4 years, 2 months ago

Comments

(Forgotten your password?)