Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在django模板的下拉列表中显示已选项目_Django_Django Templates_Multi Select - Fatal编程技术网

在django模板的下拉列表中显示已选项目

在django模板的下拉列表中显示已选项目,django,django-templates,multi-select,Django,Django Templates,Multi Select,嗨,我有一个这样的下拉列表 <select name="category" data-placeholder="select Category here" multiple class="chosen-select" tabindex="8" required> <option value=""></option> <option>

嗨,我有一个这样的下拉列表

           <select name="category" data-placeholder="select Category here" multiple
                class="chosen-select" tabindex="8" required>
                <option value=""></option>
                <option>Transport</option>
                <option>Accommodation</option>
                <option>Ware House</option>
                <option>Readymade</option>
            </select>
 {% for category in categories %}
    <option value=""></option>
    <option value="{{ category.category }}"{% if category.category == 'Transport' %}selected{% endif %}>Transport</option>
    <option value="{{ category.category }}"{% if category.category == 'Accommodation' %}selected{% endif %}>Accommodation</option>
    <option value="{{ category.category }}"{% if category.category == 'Activity' %}selected{% endif %} >Activity</option>
   <option  value="{{ category.category }}"{% if category.category == 'Readymade' %}selected{% endif %}>Pre Packaged Plan</option>
                         </option>
                     {% endfor %}
当我这样做的时候

           <select name="category" data-placeholder="select Category here" multiple
                class="chosen-select" tabindex="8" required>
                <option value=""></option>
                <option>Transport</option>
                <option>Accommodation</option>
                <option>Ware House</option>
                <option>Readymade</option>
            </select>
 {% for category in categories %}
    <option value=""></option>
    <option value="{{ category.category }}"{% if category.category == 'Transport' %}selected{% endif %}>Transport</option>
    <option value="{{ category.category }}"{% if category.category == 'Accommodation' %}selected{% endif %}>Accommodation</option>
    <option value="{{ category.category }}"{% if category.category == 'Activity' %}selected{% endif %} >Activity</option>
   <option  value="{{ category.category }}"{% if category.category == 'Readymade' %}selected{% endif %}>Pre Packaged Plan</option>
                         </option>
                     {% endfor %}
{%用于类别中的类别%}
运输
住宿
活动
预先包装的计划
{%endfor%}

在这种情况下,例如,若我在数据库中选择了2个选项,那个么它会打印两次选项,但选择的结果是正确的。非常感谢您的帮助。

如果
类别
是您要选择的类别的列表,那么您可以将其列为一个列表(
类别名称=[category.category for categories in categories]
),并且在HTML中,不要迭代
类别
(这将导致N倍的类别),但要检查每个选项是否在所选列表中:

<select ...>
    <option value=""></option>
    <option value="Transport" {% if 'Transport' in category_names %}selected{% endif %}>Transport</option>
    <option value="Accommodation" {% if 'Accommodation' in category_names %}selected{% endif %}>Accommodation</option>
    ...etc
</select>

运输
住宿
等

现在,如果您需要使用类别名称动态填充此
,这是另一个问题,但这不是我对此问题的解释。

如果
类别
是您要选择的类别的列表,那么您可以将其作为一个列表(
类别名称=[category.category for categories in categories]
),在HTML中,不要迭代
类别(这将导致类别的N倍),而是检查每个选项是否在所选列表中:

<select ...>
    <option value=""></option>
    <option value="Transport" {% if 'Transport' in category_names %}selected{% endif %}>Transport</option>
    <option value="Accommodation" {% if 'Accommodation' in category_names %}selected{% endif %}>Accommodation</option>
    ...etc
</select>

运输
住宿
等

现在,如果您需要使用类别名称动态填充此
,这是另一个问题,但这不是我对此问题的解释。

这是因为您正在迭代您的类别,并且在每次迭代中添加4个选项。是的,我知道了。有什么解决方案吗?@PedramParsian在这个场景中,这是因为您正在迭代ov在每个迭代中,添加4个选项。是的,我知道了。有什么解决方案吗?@PedramParsian在这个场景中