recherche

Maison  >  Questions et réponses  >  le corps du texte

django - python if-elif-else 结构简化

    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控制流改写?

大家讲道理大家讲道理2770 Il y a quelques jours721

répondre à tous(2)je répondrai

  • 巴扎黑

    巴扎黑2017-04-17 17:52:59

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

    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

    répondre
    0
  • 迷茫

    迷茫2017-04-17 17:52:59

    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?巭孬啊少年

    répondre
    0
  • Annulerrépondre