ページネーションの部品をテンプレートファイルで作成する - Django

別の HTML ファイルを読み込みたい場合

表示している HTML ファイルに、別の HTML ファイルを読み込ませたい場合、include を使い、読み込みたい HTML ファイルを指定します。

テンプレートの HTML の一部をモジュールのように部品化して使いまわしたい場合などに使うと便利です。

pythonApp\class_crud\class_crud\books\templates\books\list.html

{% extends "books/base.html" %}
{% block title %}books/list view{% endblock %}

{% block content %}
<h1>books/list</h1>
<p><a href="{% url 'books:create' %}">Books Add</a></p>
<table>
  <form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <tr>
      <th></th>
      <td><input type="submit" value="click"></td>
    </tr>
  </form>
</table>
<table>
  <thead>
      <th>ID</th>
      <th>タイトル</th>
      <th>body</th>
      <th></th>
  </thead>
  <tbody>
      {% for obj in books_list %}
      <tr>
          <td>{{ obj.id }}</td>
          <td>{{ obj.title }}</td>
          <td>{{ obj.body }}</td>
          <td>
            <a href="{% url 'books:show' obj.pk %}">Show</a>
            <a href="{% url 'books:update' obj.pk %}">Edit</a>
            <a href="{% url 'books:delete' obj.pk %}">Delete</a>
          </td>
      </tr>
      {% endfor %}
  </tbody>
</table>

{% include './pagenation.html' %}

{% endblock %}

pythonApp\class_crud\class_crud\books\templates\books\pagenation.html

<div class="pagination">
  <ul class="clearfix">
    {% if page_obj.has_previous %}
    <li><a href="?page=1">first</a></li>
    <li><a href="?page={{page_obj.previous_page_number}}">prev</a></li>
    {% endif %}

    {% if not page_obj.has_previous %}
    <li>first</li>
    <li>prev</li>
    {% endif %}

    <li><span class="current">[ {{page_obj.number}}/{{page_obj.paginator.num_pages}} ]</span></li>

    {% if page_obj.has_next %}
    <li><a href="?page={{page_obj.next_page_number}}">next</a></li>
    <li><a href="?page={{page_obj.paginator.num_pages}}">last</a></li>
    {% endif %}

    {% if not page_obj.has_next %}
    <li>next</li>
    <li>last</li>
    {% endif %}
  </ul>
</div><!-- /div.pagination -->