【发布时间】:2016-06-15 13:19:22
【问题描述】:
我有一个 django 项目,我正在尝试使用optgroup 标签填充选择下拉列表,然后在这些标签下填充项目列表。
所以在我的模板中我有 2 个变量,buildings 和 locations
然后在我的模板中,我迭代所有位置并创建一个optgroup 标签,然后在该标签下我得到所有与location 相关的buildings,但由于某种原因,我的选择从不包含带有我的位置的optgroup 标签
#view.py
buildings = TBuildings.objects.order_by('ixLocation')
locations = TLocations.objects.all()
#dropdown.html
<div class="dropdown">
<!-- test to see if it actually works and it does -- >
{% for location in locations %}
<optgroup label="{{ location.asLocation }}">
</optgroup>
{% endfor %}
<!-- this doesn't -->
<select class="selectpicker" data-width="100%" data-live-search="true" style="border-radius: 0px;">
{% for location in locations %}
<optgroup label="test">
</optgroup>
{% endfor %}
</select>
</div>
第一个 for 循环只是为了看看它是否会创建 optgroup 并且确实会创建,但在我的选择中它不会
编辑 我在意识到它不起作用之前尝试使用的代码:
<div class="dropdown">
<select class="selectpicker" data-width="100%" data-live-search="true" style="border-radius: 0px;">
{% for location in locations %}
<optgroup label="{{ location.asLocation }}">
{% for building in buildings %}
{% if building.ixLocation == location.ixLocation %}
<option>#{{building.iBuildingNum}} - {{building.asBuildingName}}</option>
{% endif %}
{% endfor %}
</optgroup>
{% endfor %}
</select>
</div>
【问题讨论】:
-
你的
select标签错误 -
怎么回事?当我手动添加标签时它可以工作
标签: html django jinja2 bootstrap-select