Tuesday, May 21, 2013

double loop in Djano template

tentative solution

<table class="table table-striped table-bordered">
    <tr>
        <td>-</td>
        {% for timeFrame in timeFrames %}
            <td >{{ timeFrame }}</td>
        {% endfor %}
    </tr>
    {% for code, report in reports.items %}
        <tr>
            <td>{{ code }}</td>
            {% for key, value in report.items %}
                <td>{% if value.0 %}{{ value.0 }} ({{ value.1 }}%){% endif %}</td>
            {% endfor %}
        </tr>
    {% endfor %}
    </table>






def daily_report(request, terminal):
    dict = {}
    reports = {}
    dict['date'] = request.session.get('date', '')
    if not dict['date']:
        dict['error'] = "Please select date first"
    else:
        dict['header'] = terminal_dict[terminal]
        dict['timeFrames'] = [i for i in xrange(48)]
        blocks = Block.objects.filter(terminal_code=terminal)
        for block in blocks:
            reports[block.code] = block.generate_daily_report(request.session['date'])

        dict['blocks'] = blocks
        dict['reports'] = reports
    return render_to_response(
        'daily_report.html',
        dict,
        context_instance = RequestContext(request)
    )

No comments:

Post a Comment