在Django的for循环中使用if else条件

y53ybaqx  于 2023-03-09  发布在  Go
关注(0)|答案(1)|浏览(149)

我刚接触Django,我有一个表中显示的用例模型列表,以及一个包含用例外键的用例进度模型。
最初一个新的用例还没有进度,所以在我的模板中,我试图说检查这个用例是否还没有进度,并设置一个默认值,这样进度列就不会是空的,看起来很奇怪。
我好像写不出我的逻辑文字。我希望我说的够清楚。
我的模板:

{% 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 is not none and 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: 60%;" aria-valuenow="60" 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 %}
                                    <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>
                                        {% for progress in result.usecaseids.all %}
                                        <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: 60%;" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"></div>
                                            </div>
                                            </div>
                                        </td>
                                        <td>NEW</td>
                                        <td>NEW</td>
                                        <td>NEW</td>
                                        <!-- <td>{{progress.estimated_date_delivery}}</td> -->
                                        {% endfor %}
                                    </tr>
                                    {% 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 %}
bvuwiixz

bvuwiixz1#

可以使用if语句检查result对象是否具有相关的usecase_progress对象,如果不存在,则设置默认值,如下所示:

{% 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 %}

相关问题