Created
June 30, 2019 13:24
-
-
Save Kuzyashin/18e5515d5270cffdc614dc7d51409a46 to your computer and use it in GitHub Desktop.
django_pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageListView(ListView): | |
model = Image | |
template_name = "main/image_list.html" | |
paginate_by = 10 | |
def get_context_data(self, **kwargs): | |
context = super(ImageListView, self).get_context_data(**kwargs) | |
image_list = Image.objects.all() | |
paginator = Paginator(image_list, self.paginate_by) | |
page = self.request.GET.get('page') | |
try: | |
image_list = paginator.page(page) | |
except PageNotAnInteger: | |
image_list = paginator.page(1) | |
except EmptyPage: | |
image_list = paginator.page(paginator.num_pages) | |
context['image_list'] = image_list | |
return context |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment