Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // views.py
- class Home(ListView):
- model = Post
- template_name = 'blog/index.html'
- context_object_name = "posts"
- paginate_by = 10
- def get_queryset(self):
- return Post.objects.filter(is_book_progress=False).filter(is_published=True)
- def get_context_data(self, *, object_list=None, **kwargs):
- context = super().get_context_data(**kwargs)
- context['title'] = 'Главная'
- context['page_obj'] = Post.objects.all()
- paginator = Paginator(context['page_obj'], 1)
- page = self.GET.get('page')
- try:
- context['page_obj'] = paginator.page(page)
- except PageNotAnInteger:
- context['page_obj'] = paginator.page(1)
- except EmptyPage:
- context['page_obj'] = paginator.page(paginator.num_pages)
- return context
- // paginator.html
- {% if page_obj.has_other_pages %}
- <nav class="paginator">
- {% if page_obj.has_previous %}
- <a class="arrow left" href="?page=1"><i class="fas fa-angle-double-left"></i></a>
- <a class="arrow left" href="?{{ s }}page={{ page_obj.previous_page_number }}"><i class="fas fa-angle-left"></i></a>
- {% endif%}
- {% for p in page_obj.paginator.page_range %}
- {% if page_obj.number == p %}
- <a class="page_number active">{{ p }}</a>
- {% elif p > page_obj.number|add:-3 and p < page_obj.number|add:3 %}
- <a class="page_number" href="?{{ s }}page={{ p }}">{{ p }}</a>
- {% endif %}
- {% endfor %}
- {% if page_obj.has_next %}
- <a class="arrow right" href="?{{ s }}page={{ page_obj.next_page_number }}"><i class="fas fa-angle-right"></i></a>
- <a class="arrow right" href="?{{ s }}page={{ page_obj.paginator.num_pages }}"><i class="fas fa-angle-double-right"></i></a>
- {% endif%}
- </nav>
- {% endif %}
- // base.html
- <script>
- function AjaxPagination(){
- $("a.page_number").each((index, el) => {
- $(el).click((e) => {
- e.preventDefault();
- let page_url = $(el).attr('href');
- console.log(page_url);
- $.ajax({
- url: page_url,
- type: "GET",
- sucess: (data) => {
- $(".articles").empty();
- $(".articles").append($(data).filter(".articles").html());
- $(".paginator").empty();
- $(".paginator").append($(data).filter(".paginator").html());
- }
- })
- });
- });
- }
- $(document).ready(function() {
- AjaxPagination();
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement