Skip to content

Commit

Permalink
Issue #19: Add paging to article lists
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerog committed Mar 4, 2014
1 parent 9f3b739 commit 5061a33
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
38 changes: 33 additions & 5 deletions articles/templates/article_list.html
Expand Up @@ -46,11 +46,39 @@ <h3><a href="{{ article.id }}">{{ article.title }}</a></h3>
<hr>

<div class="row">

<ul class="pager">
<li class="previous"><a href="?page=0">&larr; Older</a></li>
<li class="next"><a href="?page=2">Newer &rarr;</a></li>
</ul>
<div class="col-lg-12">
<ul class="pagination">
{% if prev_page == 0 %}

{% else %}
{% if brothers_count %}
<li><a href="?page={{ prev_page }}&count={{ brothers_count }}">&laquo;</a></li>
{% else %}
<li><a href="?page={{ prev_page }}">&laquo;</a></li>
{% endif %}
{% endif %}
{% for i in page_numbers %}
{% if page_number == i %}
<li class="active"><a href="#">{{ page_number }}</a></li>
{% else %}
{% if brothers_count %}
<li><a href="?page={{ i }}&count={{ brothers_count }}">{{ i }}</a></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if next_page == 0 %}

{% else %}
{% if brothers_count %}
<li><a href="?page={{ next_page }}&count={{ brothers_count }}">&raquo;</a></li>
{% else %}
<li><a href="?page={{ next_page }}">&raquo;</a></li>
{% endif %}
{% endif %}
</ul>
</div>

</div>

Expand Down
27 changes: 26 additions & 1 deletion articles/views.py
Expand Up @@ -2,8 +2,15 @@

from django.http import HttpResponse
from django.template import Context, loader
import math

from articles.models import Article, ArticleCategory, ArticleEntity, Gallery, InGallery, Picture
from listing.pages import PageHelper

default_count_per_page = 5
max_count_per_page = 25
max_pages_listed_on_screen = 5


def index(request):
t = loader.get_template('article_list.html')
Expand All @@ -17,7 +24,25 @@ def general_listing(request, eventType, category):
type_id = ArticleCategory.objects.filter(name=category)[0].id
t = loader.get_template('article_list.html')
article_list = Article.objects.filter(category=type_id)
c = Context({'eventType': eventType, 'article_list': article_list})
article_count = PageHelper.get_request_count(request, default_count_per_page, max_count_per_page)
page_number = PageHelper.get_page_number(request)
articles_range_min = (page_number - 1) * article_count
articles_range_max = (page_number) * article_count
number_of_articles = len(article_list)
total_pages = int(math.ceil(number_of_articles / float(article_count)))
article_list = article_list[articles_range_min:articles_range_max]
page_numbers_list = PageHelper.calculate_page_range(total_pages, page_number, max_pages_listed_on_screen)
next_page = page_number + 1 if number_of_articles > articles_range_max else 0
prev_page = page_number - 1
context_dict = {
'eventType': eventType,
'article_list' : article_list,
'page_number' : page_number,
'prev_page': prev_page,
'next_page' : next_page,
'page_numbers' : page_numbers_list
}
c = Context(context_dict)
return HttpResponse(t.render(c))


Expand Down

0 comments on commit 5061a33

Please sign in to comment.