# HG changeset patch # User Steve Losh # Date 1234465986 18000 # Node ID a4cf6f60dd02b2566cd6a6e0851ef1df4df30721 # Parent ef26da70b954cdb4403820eb1ae5f6d9beec045c The blog now uses Djangos paginator. diff -r ef26da70b954 -r a4cf6f60dd02 blog/views.py --- a/blog/views.py Thu Feb 12 14:02:55 2009 -0500 +++ b/blog/views.py Thu Feb 12 14:13:06 2009 -0500 @@ -3,11 +3,11 @@ from django.shortcuts import get_object_or_404, render_to_response from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect from django.core.urlresolvers import reverse +from django.core.paginator import Paginator from akismet import Akismet from stevelosh import deploy ak = Akismet(deploy.AKISMET_API_KEY, blog_url='http://stevelosh.com/') -ENTRIES_PER_PAGE = 10 def entry(request, year, month, day, slug): entry = get_object_or_404(Entry, slug=slug, pub_date__year=year, @@ -21,16 +21,16 @@ return HttpResponsePermanentRedirect(reverse('blog-entry', args=(year, month, day, slug))) -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 - entries = Entry.objects.all().order_by('-pub_date') - entries = entries.filter(published=True)[start_index:end_index] + + entries = Entry.objects.all().filter(published=True).order_by('-pub_date') + p = Paginator(entries, 7, orphans=4).page(page) + return render_to_response('blog/list.html', - { 'entries': entries, - 'older_page': page+1 if end_index < Entry.objects.count() else None, - 'newer_page': page-1 if page != 0 else None } ) + { 'entries': 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 } ) def comment(request): fields = request.POST diff -r ef26da70b954 -r a4cf6f60dd02 site-media/style/blog.css --- a/site-media/style/blog.css Thu Feb 12 14:02:55 2009 -0500 +++ b/site-media/style/blog.css Thu Feb 12 14:13:06 2009 -0500 @@ -33,6 +33,11 @@ float: right; } +div#blog-list-nav { + border: 0px; + margin-left: -1.5em; +} + diff -r ef26da70b954 -r a4cf6f60dd02 templates/blog/list.html --- a/templates/blog/list.html Thu Feb 12 14:02:55 2009 -0500 +++ b/templates/blog/list.html Thu Feb 12 14:13:06 2009 -0500 @@ -2,41 +2,41 @@ {% load typogrify %} {% block content %} -
- {% for entry in entries %} -
-

- - {{ entry.title|typogrify }} - -

-

{{ entry.snip|typogrify }}

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

- Newer » -

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

- - « Older - -

- {% else %} -

 

- {% endifnotequal %} -
- {% endifnotequal %} -
+
+ {% for entry in entries %} +
+

+ + {{ entry.title|typogrify }} + +

+

{{ entry.snip|typogrify }}

+
+ {% endfor %} + + {% ifnotequal newer_page older_page %} +
+ {% ifnotequal newer_page None %} + {% ifequal newer_page 0 %} + {% url blog-list-newest as new_page %} + {% else %} + {% url blog-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