目次

Django

雑感

since 2021-06-30

ListView + paginator + get_elided_page_range

since 2021-08-20

generic.ListView の paginator で使う方法を試行錯誤した結果が下記

class HogeListView(generic.ListView):
 
    template_name = "hoge/list.html"
    paginate_by = 10
    model = Hoge
    ordering = ["id"]
 
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["page_range"] = context["paginator"].get_elided_page_range(
            context["page_obj"].number
        )
        return context
/* pagination.html 部分テンプレート */
/* context["page_range"] がない場合は page_obj.paginator.page_range を使用する */
 
{% for i in page_range|default_if_none:page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li class="page-item active" aria-current="page">
    <a class="page-link" role="button" aria-disabled="true">{{ i }}</a>
</li>
{% elif i == page_obj.paginator.ELLIPSIS %}
<li class="page-item">
    <a class="page-link">{{ i }}</a>
</li>
{% else %}
<li class="page-item">
    <a class="page-link" href="?page={{ i }}">{{ i }}</a>
</li>
{% endif %}
{% endfor %}

2018年(書きかけ)

since 2018-05-30

そういえば記事を作っていなかった。

https://djangogirlsjapan.gitbooks.io/workshop_tutorialjp/django_installation/

WSL Ubuntu 16.04

まずは Python 3.6 を入れよう

https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6

$ python3 -V
Python 3.5.2

$ python3.6 -V
Python 3.6.5

だが pip がない

$ python3.6 -m pip
/usr/bin/python3.6: No module named pip

https://qiita.com/TKYK38/items/2a73696a9d8f1f856f68

https://pip.pypa.io/en/stable/installing/

こうするのか

$ sudo apt-get install python3.6-dev
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ sudo python3.6 get-pip.py

$ python3.6 -m pip -V
pip 10.0.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)

これでよし