Django:再次编辑页面,下拉框默认选中

<select data-placeholder="选择项目..." class="form-control" name="db_link_name" id="db_link"
        required="required">
    <option value="">请选择数据库连接</option>
    {% for item in res %}
        <option value="{{ item.id }}" {% if item.id == case_info.db_link %}
                selected="selected" {% endif %} hassubinfo="true">{{ item.link_name }}</option>
    {% endfor %}
</select>

核心在于 {% if item.id == case_info.db_link %}  selected="selected"  {% endif %}

判断下拉选项中的value值哪个背选中,选中的增加selected属性。

其中case_info来自于另一组数据,如下:

case_id = request.GET.get('case_id')
case_info = ComparisonCaseInfo.objects.get(id=case_id).case_info
res = get_db_config()return render(request, 'comparison/reedit_comparison_form.html',
                      {'res': res, 'case_info': json.loads(case_info)})

猜你喜欢

转载自www.cnblogs.com/gcgc/p/12161689.html