django - python if-elif-else 结构简化
大家讲道理
大家讲道理 2017-04-17 17:50:59
0
2
726
    per_page = 15
        p = Paginator(logs, per_page)
        if self.page > p.num_pages:
            self.page = p.num_pages
            self.next_page = False
            self.end_page = True
            self.next_next_page = False
        elif self.page == p.num_pages:
            self.next_page = False
            self.next_next_page = False
            self.end_page = True
        elif self.page == p.num_pages-1:
            self.next_page = True
            self.next_next_page = False
            self.end_page = True
        elif self.page == p.num_pages-2:
            self.next_page = True
            self.next_next_page = True
            self.end_page = True
        else:
            self.next_page = True
            self.next_next_page = True
            self.end_page = False

        if per_page < p.count:
            self.paginator_active = True
        else:
            self.paginator_active = False
        page = p.page(self.page)
        self.pages = p.num_pages
        return page.object_list

django view视图模块里使用到的分页代码,如何用更方便的python控制流改写?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

membalas semua(2)
巴扎黑

先不说你其他代码,只针对你贴出来的代码

per_page = 15
p = Paginator(logs, per_page)

self.next_page = False
self.next_next_page = False
self.end_page = True
self.paginator_active = True if per_page < p.count else False

if self.page > p.num_pages:
    self.page = p.num_pages
elif self.page == p.num_pages-1:
    self.next_page = True
elif self.page == p.num_pages-2:
    self.next_page = True
    self.next_next_page = True
else:
    self.next_page = True
    self.next_next_page = True
    self.end_page = False

page = p.page(self.page)
self.pages = p.num_pages
return page.object_list
迷茫
self.page = min(p.num_pages, self.page)
self.next_page = self.page < p.num_pages
self.next_next_page = self.page < p.num_pages - 1
self.end_page = p.num_pages - self.page < 3
self.paginator_active = per_page < p.count

这种东西哪用if?巭孬啊少年

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan