[Fixed]-Create a select in template from list

1👍

When you use list.split it will split on space that’s causing issue.

And Django doesn’t provide split template tag so you cannot pass argument on split (i.e. split on “,”).

You can write your own custom template tag, Read from here, https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/

Template Tag:

from django.template.defaulttags import register


@register.filter()
def split(value, arg):
    return value.split(arg)

Template:

{% load split %}
  {% with 'By Visit, By Patient'|split:"," as list %}
    <div class="selector_healthApp">
      <div class="container">
        <div class="row">
          <div class="col-xs-3">
            <label for="sel1">Select Categoty:</label>
            <select class="form-control" id="item_project">
              {% for cat_name in list %}
                <option>{{ cat_name }}</option>
              {% endfor %}
            </select>
          </div>
        </div>
      </div>
    </div>
  {% endwith %}
👤Anurag

Leave a comment