Jinja for 循环不会填充 select 下拉列表
Jinja for loop wont populate select dropdown
我有一个 Django 项目,我试图用 optgroup
标签填充 select 下拉菜单,然后在这些标签下填充项目列表。
所以在我的模板中我有 2 个变量,buildings
和 locations
然后在我的模板中,我遍历所有位置并创建一个 optgroup
标签,然后在该标签下我得到所有与 location
相关的 buildings
但由于某种原因,我的 select 从未包含带有我的位置的 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
,它确实创建了,但在我的 select 中它没有
编辑
我在意识到它不起作用之前尝试使用的代码:
<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>
愚蠢的我。
{% if building.ixLocation == location.ixLocation %}
应该是
{% if building.ixLocation.ixLocation == location.ixLocation %}
因为 building.ixLocation 生成外键字段的名称,所以我的 optgroup
标签下的列表是空的,这反过来不会在 select
[=14= 中显示任何内容]
我有一个 Django 项目,我试图用 optgroup
标签填充 select 下拉菜单,然后在这些标签下填充项目列表。
所以在我的模板中我有 2 个变量,buildings
和 locations
然后在我的模板中,我遍历所有位置并创建一个 optgroup
标签,然后在该标签下我得到所有与 location
相关的 buildings
但由于某种原因,我的 select 从未包含带有我的位置的 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
,它确实创建了,但在我的 select 中它没有
编辑 我在意识到它不起作用之前尝试使用的代码:
<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>
愚蠢的我。
{% if building.ixLocation == location.ixLocation %}
应该是
{% if building.ixLocation.ixLocation == location.ixLocation %}
因为 building.ixLocation 生成外键字段的名称,所以我的 optgroup
标签下的列表是空的,这反过来不会在 select
[=14= 中显示任何内容]