Skip to content

Commit 5a6164b

Browse files
committed
add pagination in home page
1 parent d46885f commit 5a6164b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

config/blog/templates/blog/home.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,52 @@ <h1 class="fw-bolder">shahriaarrr Blog</h1>
99
<h5 class="text-muted">Write about everything that i liked</h5>
1010
</div>
1111
</div>
12+
13+
{% for object in object_list %}
14+
<div class="row">
15+
<div class="col-md-6 col-12 mx-md-auto mx-3 mt-5 md-5">
16+
<h3 class="fw-bold">{{ object.title }}</h3>
17+
<p class="text-muted">{{ object.date }}</p>
18+
<p class="text-muted">By {{ object.author.username }}</p>
19+
{% if object.likes.count == 1 %}
20+
<p class="text-muted">{{ object.likes.count }} person liked</p>
21+
{% else %}
22+
<p class="text-muted">{{ object.likes.count }} people liked</p>
23+
{% endif %}
24+
<div class="my-3">
25+
{{ object.content | truncatewords_html:50 | safe }}
26+
</div>
27+
<a href="#">Read more</a>
28+
</div>
29+
</div>
30+
{% endfor %}
31+
32+
<nav>
33+
<ul class="pagination">
34+
<div class="mx-auto d-flex flex-row mt-5">
35+
{% if page_obj.has_previous %}
36+
<li class="page-item">
37+
<a class="page-link" href="?page=1">&laquo; First
38+
</a>
39+
</li>
40+
41+
<li class="page-item">
42+
<a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
43+
</li>
44+
{% endif %}
45+
46+
<li class="page-item">
47+
<a class="page-link" href="#">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</a>
48+
</li>
49+
{% if page_obj.has_next %}
50+
<li class="page-item">
51+
<a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
52+
</li>
53+
<li class="page-item">
54+
<a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last &raquo;</a>
55+
</li>
56+
{% endif %}
57+
</div>
58+
</ul>
59+
</nav>
1260
{% endblock %}

config/blog/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Index(ListView):
99
model = Article
1010
queryset = Article.objects.all().order_by("-date")
1111
template_name = 'blog/home.html'
12+
paginate_by = 1
1213

1314

1415
class About(View):

0 commit comments

Comments
 (0)