当该字段为下拉列表时,我很努力在页面重新加载时将旧数据保存在表单中(如果用户未成功填写整个表单,否则会收到验证错误)。这是我当前的代码:
<div class="form-group">
<select class="form-control" name="primary_instrument" id="primary_instrument" placeholder="Primary instrument"><option {% if old_data.primary_instrument %} selected {% endif %} disabled>Primary instrument</option>
{% for instrument in instruments %}
<option value='{{ instrument.id }}'>{{ instrument }}</option>
{% endfor %}
</select>
<div class="error-message">
<p>
{% if 'primary_instrument' in errors %}
{{ errors.primary_instrument }}
{% endif %}
</p>
</div>
</div>
谢谢!