Django: paginated in different ways on page

On the negative side, I use Django's object_list to handle pagination. I am happy to go to the appropriate Paginator () class if you think I need it after you hear my problem:

On the main page, I want to break the pages into 7 , but on all the other pages I want to break the pages into 10 .

How can I do it? I really can't get around it. The closest thing I need to get it to work has led to the fact that a whole page of results has been excluded, so obviously I don't want this.

I would really appreciate any answers. Let me know if you need more information. Many thanks.

Joe

+3
source share
4 answers

Simply from django.core.paginator import Paginatorcreate a paginator object as p = Paginator(thestuff, 7)in the home page view p = Paginator(thestuff, 10)everywhere. Then, in any case, link pin the context that you use to render the template. p.object_listwill be set appropriately anyway (and you seem to be saying that the approach you are using is correct? Ie, what do you mean by "Django object_list"?).

Django docs have great details and examples (if you're at 1.0 or better). If you can't make it work, can you show us (the simplified version that still fails) your template and view the code?

: , , Django :

from django.core.paginator import Paginator, Page

class MyPaginator(Paginator):

  def __init__(self, **kw):
    self.deltafirst = kw.pop('deltafirst', 0)
    Paginator.__init__(self, **kw)

  def page(self, number):
    "Returns a Page object for the given 1-based page number."
    number = self.validate_number(number)
    if number == 1:
      bottom = 0
      top = self.per_page - self.deltafirst
    else:
      bottom = (number - 1) * self.per_page - self.deltafirst
      top = bottom + self.per_page
    if top + self.orphans >= self.count:
      top = self.count
    return Page(self.object_list[bottom:top], number, self)

MyPaginator , , Django own, , deltafirst=3, 3 ( 10). , paginator 10, deltafirst 3, 3 .

( validate_number , , - , MyPaginator ).

+6

object_list() Paginator . , "" (7 8)... , .

: . :-) " ", Paginator.

... > . 1, - row_list. , (row_list) None something row_list.insert(0, None)

Paginate... Paginator

+ ... . . 1. / row_list. mucky, ​​ . .

+1

, , -, , , unittest .

, _get_num_pages self.orphans + self.deltafirst, .

def _get_num_pages(self):
    """Returns the total number of pages."""
    if self._num_pages is None:
        if self.count == 0 and not self.allow_empty_first_page:
            self._num_pages = 0
        else:
            hits = max(1, self.count - self.orphans + self.deltafirst)
            self._num_pages = int(ceil(hits / float(self.per_page)))
    return self._num_pages
num_pages = property(_get_num_pages)

unittest:

def test_ads_paginator(self):
    per_page = 10
    num_items = 100
    num_firstpage = 5
    expected_num_pages = 11

    items = range(num_items)
    paginator = AdsPaginator(items, per_page, deltafirst=per_page-num_firstpage)
    last_item = paginator.page(paginator.num_pages).object_list[-1]

    self.assertEqual(len(paginator.page(1)), num_firstpage)
    self.assertEqual(paginator.num_pages, expected_num_pages)
    self.assertEqual(last_item, items[-1])
0

1 ( ). , - paginator .

    def pages_iter():
        page_num = 1
        num_records = lambda x: 30 if x > 1 else 10
        end_idx = 0

        while True:
            start_idx = end_idx
            end_idx += num_records(page_num)
            yield {
                'number': page_num,
                'object_list': donors[start_idx:end_idx]
            }

            if end_idx > donors.count() - 2:
                break
            page_num += 1

Add { pages: page_iter }to the context of your page, and then you have the paginator variable. Note that num_recordsthis is a function that accepts page_number, so you really have maximum flexibility.

In your template, you can consider this the same as for paginator:

{% for page in pages %}
  {% if page.number == 1 %}
    {{ header }}
  {% endif %}

  {% for obj in page.object_list %}
    {{ obj }}
  {% endfor %}
{% endfor %}
0
source

Source: https://habr.com/ru/post/1711437/


All Articles