Skip to content

Commit

Permalink
first cut at pagination
Browse files Browse the repository at this point in the history
In the template, I had actually hoped to use the {%
bootstrap_pagination %} template tags provided by the
django-bootstrap3 app that we're using, but the documentation was
uninformative on how to use it, and glancing at the source, it kind of
seems like it assumes that pages are given in the query string (am I
misunderstanding something?!), so I guess we'll just have to do it
ourselves. What we have in this commit isn't quite right (for example,
there's a link to page zero, which 500s on an EmptyPage "That page
number is less than 1" exception), but it's an okayish start. We'll
also want to abstract this into a function (as with `scored_context`).

[#21]
  • Loading branch information
zackmdavis committed Sep 27, 2014
1 parent f8609dd commit c04a226
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
17 changes: 14 additions & 3 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from django.contrib.auth.models import User
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator
from django.db import IntegrityError
from django.conf import settings

from core.models import FinetoothUser, Post, Comment, Tag
from core.colorize import stylesheet
Expand All @@ -32,11 +34,20 @@ def scored_context(scoreables, context):
})
return context

def home(request):
posts = Post.objects.all()
def home(request, page):
page = int(page) if page else 1
all_posts = Post.objects.all()
requested = request.GET.get('results')
posts_per_page = (int(requested) if (requested and requested.isdigit())
else settings.POSTS_PER_PAGE)
paginator = Paginator(all_posts, posts_per_page)
if page > paginator.num_pages:
# pagination is 1-indexed
return redirect("home", paginator.num_pages)
posts = paginator.page(page)
return render(
request, "home.html",
scored_context(posts, {'posts': posts})
scored_context(posts, {'posts': posts, 'page': page})
)

def serve_stylesheet(request, low_score, low_color, high_score, high_color):
Expand Down
2 changes: 2 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@
},
}
}

POSTS_PER_PAGE = 15
5 changes: 5 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@
</a></p>
</div>
{% endfor %}

<ul class="pager">
<li><a href="/page/{{ page|add:"-1" }}">Previous</a></li>
<li><a href="/page/{{ page|add:"1" }}">Next</a></li>
</ul>
{% endblock %}
2 changes: 1 addition & 1 deletion urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

urlpatterns = patterns(
'',
url(r'^$', 'core.views.home', name='home'),
url(r'^(?:page/(\d+)/)?$', 'core.views.home', name='home'),
url(r'^valuation_(-?\d+)-([0-9a-f]{6})_(-?\d+)-([0-9a-f]{6}).css$',
'core.views.serve_stylesheet', name='stylesheet'),
url(r'^(\d+)/$', 'core.views.show_post', name="show_post"),
Expand Down

0 comments on commit c04a226

Please sign in to comment.