You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Django模板中用for循环+if-else处理用例进度默认值

Django模板进度列默认值设置问题

我是Django新手,现有Usecase模型列表(以表格展示),以及关联Usecase外键的UsecaseProgress模型。新建的Usecase初始无进度记录,我希望在模板中通过if-else结合for循环判断,为无进度的用例设置默认值,避免进度列空白,但当前逻辑编写有误,模板代码如下:

{% extends 'EmpDashboard.html' %}
{% block body %}

<div class="row d-flex">
    <div class="col-12 mb-4">
        <div class="card border-light shadow-sm components-section d-flex">
            <div class="card-body d-flex row col-12">
                <div class="row mb-4">
                    <div class="col-lg-12 col-sm-16">
                        <h3 class="h3 mb-4">View Usecases:</h3>
                    </div>
                    {% if usecase_details is not none and usecase_details %}
                    <div class="table-responsive">
                        <table id="example" class="table table-flush text-wrap table-sm" cellspacing="0" width="100%">
                            <thead class="thead-light">
                                <tr>
                                    <th scope="col">No.</th>
                                    <th scope="col">Usecase ID</th>
                                    <th scope="col">Usecase Name</th>
                                    <th scope="col">Client</th>
                                    <th scope="col">KPI</th>
                                    <th scope="col">Progress</th>
                                    <th scope="col">Progress date</th>
                                    <th scope="col">Pipeline</th>
                                    <th scope="col">Phase</th>
                                    <!-- <th scope="col">Estimated Delivery</th> -->
                                    <th scope="col">Details</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for result in usecase_details %}
                                <tr>
                                    <td>{{ forloop.counter }}</td>
                                    <td><span class="badge bg-info">{{result.usecase_id}}</span></td>
                                    <td>{{result.usecase_name}}</td>
                                    <td>{{result.business_owner.business_owner_name}}</td>
                                    <td>{{result.kpi.kpi_name}}</td>
                                    {% if result.usecaseids.all %}
                                    {% for progress in result.usecaseids.all %}
                                    {% if forloop.last %}
                                    <td><div class="progress-wrapper">
                                        <div class="progress-info">
                                        <div class="progress-percentage">
                                        <span>{{progress.usecase_progress_value}}%</span>
                                        </div>
                                        </div>
                                        <div class="progress">
                                        <div class="progress-bar bg-success" role="progressbar" style="width: {{progress.usecase_progress_value}}%;" aria-valuenow="{{progress.usecase_progress_value}}" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                        </div>
                                    </td>
                                    <td>{{progress.usecase_progress_date}}</td>
                                    <td>{{progress.pipeline.pipeline_name}}</td>
                                    <td>{{progress.phase.phase_name}}</td>
                                    {% endif %}
                                    {% endfor %}
                                    {% else %}
                                    <td><div class="progress-wrapper">
                                        <div class="progress-info">
                                        <div class="progress-percentage">
                                        <span>0%</span>
                                        </div>
                                        </div>
                                        <div class="progress">
                                        <div class="progress-bar bg-success" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
                                        </div>
                                        </div>
                                    </td>
                                    <td>NEW</td>
                                    <td>NEW</td>
                                    <td>NEW</td>
                                    {% endif %}
                                    <td>
                                        <a href="/view-usecase/{{result.usecase_id}}" class="btn btn-success">VIEW</a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="col-lg-12 col-sm-16">
                       
                        <h3 class="h3 text-center">
                            No Records Found!
                        </h3>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>  


<script>
$(document).ready(function () {
  $('#example').DataTable();
  $('.dataTables_length').addClass('bs-select');
  $('#example td').css('white-space','initial');
});
</script>
{% endblock %}

错误分析

  1. 表格结构错乱:原else分支中重复创建了<tr>标签,外层已经有包裹整行的<tr>,导致HTML结构不符合表格规范
  2. 无效循环:else分支中仍对空的result.usecaseids.all执行for循环,循环不会触发,默认值内容无法渲染
  3. 固定进度条宽度:进度条的width硬编码为60%,未根据实际进度值动态调整

修正说明

  1. 移除else分支内的重复<tr>,直接在原行内填充默认值列
  2. 删掉else分支中无效的for循环,直接渲染默认内容
  3. 动态绑定进度条宽度:有进度时使用{{progress.usecase_progress_value}}%,无进度时设为0%
  4. 简化判断条件:Django中空QuerySet在模板中直接判断{% if result.usecaseids.all %}即可,无需额外判断is not none

内容的提问来源于stack exchange,提问作者JHS99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 04:32:06