a4cf6f60dd02

The blog now uses Djangos paginator.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Thu, 12 Feb 2009 14:13:06 -0500
parents ef26da70b954
children 3c20caba5321
branches/tags (none)
files blog/views.py site-media/style/blog.css templates/blog/list.html

Changes

--- 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
--- 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;
+}
+
 
 
 
--- 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 %}
-	<div id="blog-list">
-		{% for entry in entries %}
-			<div class="blog-list-entry">
-				<h2 class="blog-list-entry-title">
-					<a href="{% url stevelosh.blog.views.entry entry.pub_date.year, entry.pub_date.month, entry.pub_date.day, entry.slug %}">
-						{{ entry.title|typogrify }}
-					</a>
-				</h2>
-				<p class="blog-list-entry-snip">{{ entry.snip|typogrify }}</p>
-			</div>
-		{% endfor %}
-		
-		{% ifnotequal newer_page older_page  %}
-			<div class="blog-list-entry">
-			{% 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 %}
-				
-				<h2 id="blog-list-newer"><a href="{{ new_page }}">
-					Newer &raquo;
-				</a></h2>
-			{% endifnotequal %}
-			{% ifnotequal older_page None %}
-				<h2 id="blog-list-older">
-					<a href="{% url blog-list-page older_page %}">
-						&laquo; Older
-					</a>
-				</h2>
-			{% else %}
-				<h2 id="blog-list-older">&nbsp;</h2>
-			{% endifnotequal %}
-			</div>
-		{% endifnotequal %}
-	</div>
+    <div id="blog-list">
+        {% for entry in entries %}
+            <div class="blog-list-entry">
+                <h2 class="blog-list-entry-title">
+                    <a href="{% url stevelosh.blog.views.entry entry.pub_date.year, entry.pub_date.month, entry.pub_date.day, entry.slug %}">
+                        {{ entry.title|typogrify }}
+                    </a>
+                </h2>
+                <p class="blog-list-entry-snip">{{ entry.snip|typogrify }}</p>
+            </div>
+        {% endfor %}
+        
+        {% ifnotequal newer_page older_page  %}
+            <div id="blog-list-nav" class="blog-list-entry">
+            {% 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 %}
+                
+                <h2 id="blog-list-newer"><a href="{{ new_page }}">
+                    Newer &raquo;
+                </a></h2>
+            {% endifnotequal %}
+            {% ifnotequal older_page None %}
+                <h2 id="blog-list-older">
+                    <a href="{% url blog-list-page older_page %}">
+                        &laquo; Older
+                    </a>
+                </h2>
+            {% else %}
+                <h2 id="blog-list-older">&nbsp;</h2>
+            {% endifnotequal %}
+            </div>
+        {% endifnotequal %}
+    </div>
 {% endblock %}
\ No newline at end of file