Commit 47949e17 authored by Jean Michel Rouly's avatar Jean Michel Rouly
Browse files

Added pagination.

parent 520e4b4f
......@@ -16,3 +16,7 @@ a[name] {
.form-group-bottom {
margin-bottom: 0px;
}
.pager-middle {
color: rgb(34, 34, 34);
}
......@@ -57,4 +57,72 @@ HNRS 110 • Homepage
</div>
{% endif %}
<div class="row">
<div class="col-lg-12">
<!--
<div class="text-center">
<ul class="pagination">
{% if questions.has_previous %}
<li>
<a href="?page={{ questions.previous_page_number }}">&laquo;</a>
</li>
{% else %}
<li class="disabled">
<a>&laquo;</a>
</li>
{% endif %}
{% for page in page_range %}
<li{%if page == questions.number%} class="active"{%endif%}>
<a href="?page={{page}}">{{page}}</a>
</li>
{% endfor %}
{% if questions.has_next %}
<li>
<a href="?page={{ questions.next_page_number }}">&raquo;</a>
</li>
{% else %}
<li class="disabled">
<a>&raquo;</a>
</li>
{% endif %}
</ul>
</div>
-->
<ul class="pager">
{% if questions.has_previous %}
<li>
<a href="?page={{ questions.previous_page_number }}">&larr; Previous</a>
</li>
{% else %}
<li class="disabled">
<a>&larr; Previous</a>
</li>
{% endif %}
<li class="pager-middle">
Page {{ questions.number }} of {{ questions.paginator.num_pages }}
</li>
{% if questions.has_next %}
<li>
<a href="?page={{ questions.next_page_number }}">Next &rarr;</a>
</li>
{% else %}
<li class="disabled">
<a>Next &rarr;</a>
</li>
{% endif %}
</ul>
</div>
</div>
{% endblock %}
from website.models import Question, Comment, Reply
from website.forms import QuestionForm, CommentForm, ReplyForm, FeedbackForm
from django.conf import settings
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render_to_response, get_object_or_404
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
......@@ -41,8 +42,22 @@ def submit_question(request):
@login_required
def index(request):
questions = Question.objects.all()
paginator = Paginator(questions, 10) # show 25 questions per page
page = request.GET.get('page')
try:
questions = paginator.page(page)
except PageNotAnInteger:
# if page is NaN, deliver first page
questions = paginator.page(1)
except EmptyPage:
# if page is empty, deliver last page
questions = paginator.page(paginator.num_pages)
return render_to_response('index.html', {
'questions' : Question.objects.all(),
'questions' : questions,
'page_range' : range(1, int(questions.paginator.num_pages)+1),
},
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment