Home > database >  Django HTML Dropdown with multiple values filter
Django HTML Dropdown with multiple values filter

Time:09-03

I have this below mentioned dropdown with multiple values for each option

<div >
    <label>Fruits Selected</label>
    <input type="text" readonly  id="fruits_selected" name="fruits_selected" value="{{fruits_selected.product}}" />
</div>
<div >
    <div >
        <label for="">Fruits Type</label>
        <select id="Fruits" name="Fruits" >
            <option disabled="disabled" selected="selected">Choose option</option>
            <option value="Mango,Apple,Orange"> Fresh Fruits</option>
            <option value="Onion,Tomato"> Vegetable</option>
            <option value="Apple,Beetroot,Carrot"> Diet Fruits</option>
            <option value="Apple,Grapes"> Healthy Fruits</option>
            <option value="Tomato,Mango"> Juice Items</option>
        </select>
    </div>
</div>

Via Django, I will get the fruits like Apple, Orange. If the input in the {{fruits_selected.product}} is Apple then in the dropdown, I need to get the options Fresh Fruits, Diet Fruits, Healthy Fruits since the dropdown value consist Apple

Similarly, if the option in {{fruits_selected.product}} is Tomato then in the dropdown, I need to get the options Vegetable, Juice Items since the dropdown value consist Tomato

CodePudding user response:

you can do it in template, although in view it is better to do:

if you have a multiselect:

{% with product=fruits_selected.product %} <-- Tomato? Fruits? -->
    <select id="Fruits" name="Fruits" >
        <option disabled="disabled" {% if not prodict %}selected{% endif %}>Choose option</option>
        <option {% if product in "Mango,Apple,Orange" %}selected{% endif %} value="Mango,Apple,Orange"> Fresh Fruits</option>
        <option {% if prodict in "Onion,Tomato" %}selected{% endif %} value="Onion,Tomato"> Vegetable</option>
        <option {% if prodict in "Apple,Beetroot,Carrot" %}selected{% endif %} value="Apple,Beetroot,Carrot"> Diet Fruits</option>
        <option {% if prodict in "Apple,Grapes" %}selected{% endif %} value="Apple,Grapes"> Healthy Fruits</option>
        <option {% if product in "Tomato,Mango" %}selected{% endif %} value="Tomato,Mango"> Juice Items</option>
    </select>
{% endwith %}

if you want to remove options:

{% with product=fruits_selected.product %} <-- Tomato? Fruits? -->
    <select id="Fruits" name="Fruits" >
        <option disabled selected>Choose option</option>
        {% if product in "Mango,Apple,Orange" %}<option value="Mango,Apple,Orange"> Fresh Fruits</option>{% endif %} 
        {% if prodict in "Onion,Tomato" %}<option value="Onion,Tomato"> Vegetable</option>{% endif %} 
        {% if prodict in "Apple,Beetroot,Carrot" %}<option value="Apple,Beetroot,Carrot"> Diet Fruits</option>{% endif %} 
        {% if prodict in "Apple,Grapes" %}<option value="Apple,Grapes"> Healthy Fruits</option>{% endif %} 
        {% if product in "Tomato,Mango" %}<option value="Tomato,Mango"> Juice Items</option>{% endif %} 
    </select>
{% endwith %}
  • Related