Skip to content

Commit

Permalink
Add pagination to ListView
Browse files Browse the repository at this point in the history
  • Loading branch information
huangsam committed Jul 11, 2020
1 parent dcdaef9 commit 8022588
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
16 changes: 16 additions & 0 deletions places/templates/places/restaurant_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,21 @@ <h1>Restaurants</h1>
<li>No restaurants yet.</li>
{% endfor %}
</ul>

<div class="pagination">
<div class="step-links">
<p class="current">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</p>

{% if page_obj.has_previous %}
<a class="btn btn-secondary" href="?page=1">&laquo; first</a>
<a class="btn btn-secondary" href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}

{% if page_obj.has_next %}
<a class="btn btn-secondary" href="?page={{ page_obj.next_page_number }}">next</a>
<a class="btn btn-secondary" href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
{% endif %}
</div>
</div>
</div>
{% endblock %}
5 changes: 3 additions & 2 deletions places/tests/views/test_restaurant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
from django.urls import reverse

from places.models import Restaurant
from places.views import RestaurantListView


class TestRestaurantListView(TestCase):
"""RestaurantListView test suite"""

desired_url = "/places/restaurants/"
desired_name = "places:restaurant-list"
desired_restaurant_count = 25
desired_restaurant_count = RestaurantListView.paginate_by

@classmethod
def setUpTestData(cls):
for i in range(cls.desired_restaurant_count):
for i in range(cls.desired_restaurant_count * 2):
Restaurant.objects.create(
name=f"Chipotle {i}",
address=f"Bogus {i}",
Expand Down
1 change: 1 addition & 0 deletions places/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class HomeView(TemplateView):
class RestaurantListView(ListView):
model = Restaurant
ordering = ["name"]
paginate_by = 15
context_object_name = "restaurant_list"


Expand Down

0 comments on commit 8022588

Please sign in to comment.