| | 1 | == Example views == |
| | 2 | |
| | 3 | This is an example to break up long lists into several pages. It uses one url (get) variable "?offset=xxx" |
| | 4 | {{{ |
| | 5 | def item_index(request): |
| | 6 | offset = int(request.GET.get("offset", 0)) |
| | 7 | item_count = items.get_count() |
| | 8 | limit=50 |
| | 9 | if (offset + limit) < (item_count - 1): |
| | 10 | new_offset = offset + limit |
| | 11 | else: |
| | 12 | limit= (item_count - offset) |
| | 13 | new_offset = False |
| | 14 | latest_item_list = items.get_list(order_by=['-date'], offset=offset, limit=limit) |
| | 15 | return render_to_response('item/list', { |
| | 16 | 'latest_item_list': latest_item_list, |
| | 17 | 'item_count': item_count, |
| | 18 | 'from': offset + 1, |
| | 19 | 'to': limit, |
| | 20 | 'offset': new_offset, |
| | 21 | }) |
| | 22 | }}} |
| | 23 | |
| | 24 | This is the template I use. It’s a very simple one, only displaying a row counter and a date column: |
| | 25 | |
| | 26 | {{{ |
| | 27 | {% if latest_item_list %} |
| | 28 | <p> |
| | 29 | {% if item_count %} |
| | 30 | There {% ifequal item_count "1" %}is{% endifequal %} |
| | 31 | {% ifnotequal item_count "1" %}are{% endifnotequal %} |
| | 32 | {{ item_count }} item{{ item_count|pluralize }}. |
| | 33 | {% else %} |
| | 34 | No itemcount provided to the template |
| | 35 | {% endif %} |
| | 36 | You are looking at {{ from }} to {{ to }}. |
| | 37 | </p> |
| | 38 | <table width="100%"> |
| | 39 | <tr> |
| | 40 | <th style="text-align:left;">#</th><th style="text-align:left;">Date</th> |
| | 41 | </tr> |
| | 42 | {% for item in latest_item_list %} |
| | 43 | <tr> |
| | 44 | <td>{{ forloop.counter }}</td> |
| | 45 | <td>{{ item.date }}</td> |
| | 46 | </tr> |
| | 47 | {% endfor %} |
| | 48 | </table> |
| | 49 | {% else %} |
| | 50 | <p>There are no items available.</p> |
| | 51 | {% endif %} |
| | 52 | <p> |
| | 53 | {% if offset %} |
| | 54 | <a href="?offset={{ offset }}">next</a> |
| | 55 | {% else %} |
| | 56 | End of list. |
| | 57 | {% endif %} |
| | 58 | </p> |
| | 59 | }}} |
| | 60 | |