B
B
blazer052016-04-01 16:49:46
Django
blazer05, 2016-04-01 16:49:46

How to set up a paginator?

Made a selection of pages by category i.e. when you open a category, it displays all the news of this category - it all works. I made a paginator with a limit of 5 news per page - yes, it also works, but let's say if you go to the next page in the category of 11 news and go to the next page in the pager, then an empty page is displayed, and you need to display the rest of the news in this category - how to do this?
I'm already confused with this - please help me figure it out.

def category(reguest, slug, page_number=1):
    context_dict = {'username': auth.get_user(reguest).username}
    try:
        category = Category.objects.get(slug=slug)
        context_dict['category_name'] = category.name
        pages = BlogPost.objects.filter(category=category)
        current_page = Paginator(pages, 5)
        context_dict['pages'] = pages
        context_dict['category'] = category
        context_dict['pages'] = current_page.page(page_number)
    except Category.DoesNotExist:
        pass
    return render(reguest, 'category.html', context_dict)

url(r'^pagecat/(\d+)/$', views.category), # Урл для пагинации страниц

<div class="row">
        <div class="col-lg-12" align="center">
            <ul class="pagination">
                {% if pages.has_previous %}
                    <li class="arrow"><a href="/blog/category/{{ pages.previous_page_number }}/">&laquo;</a></li>
                {% else %}
                    <li class="disabled"><a href="">&laquo;</a></li>
                {% endif %}
                {% for pag in pages.paginator.page_range %}
                    {% if pag == pages.number %}
                        <li class="active"><a href="/blog/category/{{ pag }}/">{{ pag }}</a></li>
                    {% else %}
                        <li><a href="/blog/category/{{ pag }}/">{{ pag }}</a></li>
                    {% endif %}
                {% endfor %}
                {% if pages.has_next %}
                    <li class="arrow"><a href="/blog/category/{{ pages.next_page_number }}/">&raquo;</a></li>
                {% else %}
                    <li class="disabled"><a href="">&raquo;</a></li>
                {% endif %}
            </ul>
        </div>
    </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
blazer05, 2016-04-13
@blazer05

Rewrote the function

def category(reguest, slug):
    context_dict = {}
    try:
        category = Category.objects.get(slug=slug)
        context_dict['category_name'] = category.name
        object = BlogPost.objects.filter(category=category)
        paginator = Paginator(object, 2)

        try:
            page = request.GET.get('page')
        except:
            page = 1
        try:
            objects = paginator.page(page)
        except(EmptyPage, InvalidPage):
            objects = paginator.page(paginator.num_pages)

        context_dict['objects'] = objects
        context_dict['category'] = category
        context_dict['pages'] = paginator.page(page)

    except Category.DoesNotExist:
        pass
    return render(reguest, 'category.html', context_dict)

output to template
<div class="row">
        <div class="col-lg-12" align="center">
            <ul class="pagination">
                {% if objects.has_previous %}
                    <li class="arrow"><a href="/blog/category/{{ objects.previous_page_number }}/">&laquo;</a></li>
                {% else %}
                    <li class="disabled"><a href="">&laquo;</a></li>
                {% endif %}
                {% for pag in objects.paginator.page_range %}
                    {% if pag == objects.number %}
                        <li class="active"><a href="/blog/category/{{ category.slug }}/?page={{ pag }}">{{ pag }}</a></li>
                    {% else %}
                        <li><a href="/blog/category/{{ category.slug }}/?page={{ pag }}">{{ pag }}</a></li>
                    {% endif %}
                {% endfor %}
                {% if objects.has_next %}
                    <li class="arrow"><a href="/blog/category/{{ objects.next_page_number }}/">&raquo;</a></li>
                {% else %}
                    <li class="disabled"><a href="">&raquo;</a></li>
                {% endif %}
            </ul>
        </div>
    </div>

url
all the same, it doesn’t go further than the first page, although the link changes, if you hover over the page numbers, it starts from 1, etc.
127.0.0.1:8000/blog/category/django/?page=2
where is the bug?
In general, he decided, now everything works. Here is the code that works, maybe someone will find it useful!
Function.
def category(reguest, slug):
    category = Category.objects.get(slug=slug)
    post = BlogPost.objects.filter(category=category)
    paginator = Paginator(post, 2)
    page = reguest.GET.get('page')
    try:
        post = paginator.page(page)
    except PageNotAnInteger:
        post = paginator.page(1)
    except EmptyPage:
        post = paginator.page(paginator.num_pages)
    return render(reguest, 'category.html', {
        'category': category,
        'page': page,
        'post': post})

Output to template.
<div class="row">
        <div class="col-lg-12" align="center">
            <ul class="pagination">
                {% if post.has_previous %}
                    <li class="arrow"><a href="/blog/category/{{ category.slug }}?page={{ pag }}{{ post.previous_page_number }}">&laquo;</a></li>
                {% else %}
                    <li class="disabled"><a href="">&laquo;</a></li>
                {% endif %}
                {% for pag in post.paginator.page_range %}
                    {% if pag == post.number %}
                        <li class="active"><a href="/blog/category/{{ category.slug }}?page={{ pag }}">{{ pag }}</a></li>
                    {% else %}
                        <li><a href="/blog/category/{{ category.slug }}?page={{ pag }}">{{ pag }}</a></li>
                    {% endif %}
                {% endfor %}
                {% if post.has_next %}
                    <li class="arrow"><a href="/blog/category/{{ category.slug }}?page={{ pag }}{{ post.next_page_number }}">&raquo;</a></li>
                {% else %}
                    <li class="disabled"><a href="">&raquo;</a></li>
                {% endif %}
            </ul>
        </div>
    </div>

Url
url(r'^category/(?P<slug>[-\w]+)/$', views.category, name='category'),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question