我想保持上一个Django项目登记模板表单数据。我使用姜语句,如{{OLD_DATA。[数据]}}。我没有问题,与普通的文字输入做(这样的):
TYPE Load_all.M72 > New_Load_all.M72
还有textarea(就像这样):
<div class="form-group">
<input type="text" name="zip_code" id="zip_code" class="form-control input-sm" placeholder="Zip" value="{{ old_data.zip_code }}">
</div>
但我无法弄清楚如何使用下拉菜单做。这是我当前的代码:
<textarea type="area" name="area" id="area" class="form-control
textarea" rows="3" placeholder="Please tell us briefly in what part
of town you live and how far you are willing to travel for
rehearsals and performances. i.e., 'I live in the Heights but I'm
willing to drive anywhere within the loop.'">{{ old_data.area }}
</textarea>
请帮助!
答案 0 :(得分:0)
您可以对其进行迭代,并检查每个元素的值是否对应于该选项的值,如果是这种情况,则添加一个selected
属性:
<select class="form-control" name="primary_instrument" id="primary_instrument" placeholder="Primary instrument">
<option selected disabled>Primary instrument</option>
{% for instrument in instruments %}
<option value='{{ instrument.id }}' {% if instrument.id == old_data.primary_instrument %}selected{% endif %}>{{ instrument }}</option>
{% endfor %}
</select>
请注意{% if instrument.id == old_data.primary_instrument %}selected{% endif %}
。因此,我们检查值是否相同,如果是,则将selected
属性添加到<option>
标签中。