Doge log

Abby CTO 雑賀 力王のオフィシャルサイトです

Googleっぽい改ページとか実装

Generic-View+Pagenator限定。
うーん、rangeが使えるタグがないのねえ。
改ページがなんだかなあと。
どーせ一覧なんてGeneric-View使ってPagenatorで改ページするのでタグでやってみる。
既にcontext上にPagenatorが出力している"page","pages"が出ている前提。

pagelink.py
from django.template import Node, Library, loader

register = Library()

class RangePageNode(Node):
            
    def render(self, context):
        pages = context['pages']
        page = context['page']
        
        def create_link(x):
            if x == page-1:
                return str(x+1)+' '
            else:
                return '<a href="?page='+str(x+1)+'">'+str(x+1)+'</a> '
                
        link = ''
        for i in range(pages):
            link = link+create_link(i)
        return link
        

def get_page_link(parser, token):
    tokens = token.contents.split()
    return RangePageNode()

register.tag('get_page_link', get_page_link)

汚いソースですけど・・・。
でHTML

xx_list.html
  <p>
  検索結果ページ:
  {% if previous %}
  <a href="?page={{ previous }}">前へ</a>
  {% endif %}
  {% load pagelink %}
  {% get_page_link%}
  {% if has_next %}
  <a href="?page={{ next }}">次へ</a>
  {% endif %}
  </p>

それっぽいなあ。
自己満足の世界ですわ。
でも見た目はきっとこっちの方がいいね。
うくく。