简体   繁体   中英

Looping in django template creating earror in table data

I am using a table in django template. I passed two arrays of objects in context to the template. When i am looping through the two arrays of objects, it is creating problem when the if condition is failing. When if condition is not fulfilled it places the value of later td in the previous ones. Then i tried to apply an else to provide blank td if the condition is not fulfilled. But because of else condition it creates skips the first four blocks in all conditions.

I just want to give blank spaces in first for td(table data) if the if condition is not statisfied.

 <table id="searchFilterNewAdmin" class="table table-striped table-bordered table-hover" >
    <thead>
        <tr role="row">
            <th class="sorting_asc" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending">Admin Name</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Browser: activate to sort column ascending">Tablet No</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Platform(s): activate to sort column ascending">Region</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Engine version: activate to sort column ascending">Car Make</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending">Total Time(s)</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending">Last Opened</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending">No.of Clicks</th>
            <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending">Average Time(s)</th>
        </tr>
    </thead>
    <tbody>
        {% for all in admin_time_stats %}
            <tr>
                {% for every in all_tabletadmins %}
                    {% if all.tablet_number == every.tablet_number %}
                        <td>{{ every.first_name }}</td>
                        <td>{{ every.tablet_number }}</td>
                        <td>{{ every.region }}</td>
                        <td>{{ every.car_year}}</td>
                    {% else %}
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    {% endif %}
                {% endfor %}
                <td>{{ all.total_time }}</td>
                <td>{{ all.start_time }}</td>
                <td>{{ all.no_of_clicks }}</td>
                <td>{% widthratio all.total_time all.no_of_clicks 1 %}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

admin_time_stats = [{'no_of_clicks': 15, 'tablet_number': 'tablet9', 'start_time': datetime.datetime(2017, 11, 24, 13, 57, 8, 64000, tzinfo=), 'total_time': 480}{'no_of_clicks': 1, 'tablet_number': 'tablet10', 'start_time': datetime.datetime(2017, 11, 8, 17, 18, 27, 389000, tzinfo=), 'total_time': 32}]


all_tabletadmins = [{'tablet_number': 'tablet9', 'region': 'usa', 'car_year': '1991', 'admin_name': 'sam' }]

您已在</tr>标记后添加{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM