Example views

This is an example to break up long lists into several pages. It uses one url (get) variable "?offset=xxx"

def item_index(request):
    offset = int(request.GET.get("offset", 0))
    item_count = items.get_count()
    limit=50
    if (offset + limit) < (item_count - 1):
        new_offset = offset + limit
    else:
        limit= (item_count - offset)
        new_offset = False
    latest_item_list = items.get_list(order_by=['-date'], offset=offset, limit=limit)
    return render_to_response('item/list', {
        'latest_item_list': latest_item_list,
        'item_count': item_count,
        'from': offset + 1,
        'to': limit,
        'offset': new_offset,
    })

This is the template I use. It’s a very simple one, only displaying a row counter and a date column:

{% if latest_item_list %}
    <p>
    {% if item_count %}
        There {% ifequal item_count "1" %}is{% endifequal %}
        {% ifnotequal item_count "1" %}are{% endifnotequal %}
        {{ item_count }} item{{ item_count|pluralize }}.
    {% else %}
        No itemcount provided to the template
    {% endif %}
    You are looking at {{ from }} to {{ to }}. 
    </p>
    <table width="100%">
        <tr>
            <th style="text-align:left;">#</th><th style="text-align:left;">Date</th>
        </tr>
    {% for item in latest_item_list %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ item.date }}</td>
        </tr>
    {% endfor %}
    </table>
{% else %}
    <p>There are no items available.</p>
{% endif %}
<p>
{% if offset %}
    <a href="?offset={{ offset }}">next</a>
{% else %}
    End of list.
{% endif %}
</p>
Last modified 18 years ago Last modified on Mar 10, 2006, 5:00:12 AM
Note: See TracWiki for help on using the wiki.
Back to Top