# HG changeset patch # User Steve Losh # Date 1234465007 18000 # Node ID 4503e2493b8dc33d4740e6034827f078c9316dea # Parent 54160ba670db98af56469a5cb1bc2edbaf1b18f4 Refactored Thoughts to use Djangos built-in pagination. diff -r 54160ba670db -r 4503e2493b8d site-media/style/thoughts.css --- a/site-media/style/thoughts.css Mon Feb 09 19:55:19 2009 -0500 +++ b/site-media/style/thoughts.css Thu Feb 12 13:56:47 2009 -0500 @@ -33,4 +33,8 @@ padding-left: 25px; margin-left: 14px; border-left: 1px solid #eee; +} + +span#thought-list-newer { + float: right; } \ No newline at end of file diff -r 54160ba670db -r 4503e2493b8d templates/thoughts/list.html --- a/templates/thoughts/list.html Mon Feb 09 19:55:19 2009 -0500 +++ b/templates/thoughts/list.html Thu Feb 12 13:56:47 2009 -0500 @@ -5,75 +5,75 @@ {% block title %}Thoughts{% endblock %} {% block style %} - + {% endblock %} {% block header %}/ thoughts{% endblock %} {% block content %} -
- {% for thought in thoughts %} -
- {% ifequal thought.type 'text' %} - -
-

text

-
-
- {% ifnotequal thought.title None %} -

{{ thought.title|typogrify }}

- {% endifnotequal %} - {{ thought.body|markdown|typogrify }} -
- {% endifequal %} - {% ifequal thought.type 'link' %} - -
-

link

-
- - {% endifequal %} -
- {% endfor %} - - {% ifnotequal newer_page older_page %} -
- {% ifnotequal newer_page None %} - {% ifequal newer_page 0 %} - {% url thoughts-list-newest as new_page %} - {% else %} - {% url thoughts-list-page newer_page as new_page %} - {% endifequal %} - -

- Newer » -

-
- {% endifnotequal %} - {% ifnotequal older_page None %} - -

- « Older -

-
- {% else %} - -

 

-
- {% endifnotequal %} -
- {% endifnotequal %} -
+
+ {% for thought in thoughts %} +
+ {% ifequal thought.type 'text' %} + +
+

text

+
+
+ {% ifnotequal thought.title None %} +

{{ thought.title|typogrify }}

+ {% endifnotequal %} + {{ thought.body|markdown|typogrify }} +
+ {% endifequal %} + {% ifequal thought.type 'link' %} + +
+

link

+
+ + {% endifequal %} +
+ {% endfor %} + + {% ifnotequal newer_page older_page %} +
+ {% ifnotequal newer_page None %} + {% ifequal newer_page 0 %} + {% url thoughts-list-newest as new_page %} + {% else %} + {% url thoughts-list-page newer_page as new_page %} + {% endifequal %} + +

+ Newer » +

+
+ {% endifnotequal %} + {% ifnotequal older_page None %} + +

+ « Older +

+
+ {% else %} + +

 

+
+ {% endifnotequal %} +
+ {% endifnotequal %} +
{% endblock %} \ No newline at end of file diff -r 54160ba670db -r 4503e2493b8d thoughts/views.py --- a/thoughts/views.py Mon Feb 09 19:55:19 2009 -0500 +++ b/thoughts/views.py Thu Feb 12 13:56:47 2009 -0500 @@ -1,23 +1,23 @@ from stevelosh.thoughts.models import TextThought, LinkThought from django.shortcuts import render_to_response +from django.core.paginator import Paginator import operator ENTRIES_PER_PAGE = 10 -def list(request, page=0): +def list(request, page=1): page = int(page) - start_index = page * ENTRIES_PER_PAGE - end_index = start_index + ENTRIES_PER_PAGE - total_count = TextThought.objects.count() + LinkThought.objects.count() thoughts = [] thoughts += TextThought.objects.all().order_by('-posted') thoughts += LinkThought.objects.all().order_by('-posted') thoughts.sort(key=operator.attrgetter('posted')) thoughts.reverse() - thoughts = thoughts[start_index:end_index] + + paginator = Paginator(thoughts, 5, orphans=2) + p = paginator.page(page) return render_to_response('thoughts/list.html', - { 'thoughts': thoughts, - 'older_page': page+1 if end_index < total_count else None, - 'newer_page': page-1 if page != 0 else None } ) + { 'thoughts': p.object_list, + 'older_page': p.next_page_number() if p.has_next() else None, + 'newer_page': p.previous_page_number() if p.has_previous() else None } )