Django Pagination でページが表示できず、NoReverseMatch エラーが発生する

実現したいこと

Django勉強中。paginationを行うべきプログラムしましたが、url入力してもブラウザに目的画面が表示できず、エラーが発生します。プログラムのどこが違っているのかが知り、ページ遷移を問題無く実行したい。

発生している問題・分からないこと

Django Paginationを実行すべくプログラムを組み実行。url入力してアクセスしようとすると、エラーが発生します。

エラーメッセージ

error

1NoReverseMatch at /pyapp/index_pagination/1 2Reverse for 'index-pagination' with no arguments not found. 1 pattern(s) tried: ['pyapp/index_pagination/(?P<num>[0-9]+)\\Z'] 3Request Method: GET 4Request URL: http://localhost:8000/pyapp/index_pagination/1 5Django Version: 4.1 6Exception Type: NoReverseMatch 7Exception Value: 8Reverse for 'index-pagination' with no arguments not found. 1 pattern(s) tried: ['pyapp/index_pagination/(?P<num>[0-9]+)\\Z'] 9Exception Location: C:\Users\5ta9n\anaconda3\envs\oono_latest\lib\site-packages\django\urls\resolvers.py, line 803, in _reverse_with_prefix 10Raised during: pyapp.views.index_pagination 11Python Executable: C:\Users\5ta9n\anaconda3\envs\oono_latest\python.exe 12Python Version: 3.9.18 13Python Path: 14['c:\\webdjango\\pyproject', 15 'C:\\Users\\5ta9n\\anaconda3\\envs\\oono_latest\\python39.zip', 16 'C:\\Users\\5ta9n\\anaconda3\\envs\\oono_latest\\DLLs', 17 'C:\\Users\\5ta9n\\anaconda3\\envs\\oono_latest\\lib', 18 'C:\\Users\\5ta9n\\anaconda3\\envs\\oono_latest', 19 'C:\\Users\\5ta9n\\anaconda3\\envs\\oono_latest\\lib\\site-packages', 20 'C:\\Users\\5ta9n\\anaconda3\\envs\\oono_latest\\lib\\site-packages\\win32', 21 'C:\\Users\\5ta9n\\anaconda3\\envs\\oono_latest\\lib\\site-packages\\win32\\lib', 22 'C:\\Users\\5ta9n\\anaconda3\\envs\\oono_latest\\lib\\site-packages\\Pythonwin'] 23Server time: Tue, 30 Apr 2024 14:20:23 +0900 24Error during template rendering 25In template c:\webdjango\pyproject\pyapp\templates\pyapp_sub\index_pagination.html, error at line 72 26 27Reverse for 'index-pagination' with no arguments not found. 1 pattern(s) tried: ['pyapp/index_pagination/(?P<num>[0-9]+)\\Z'] 2862 <a class="page-link"> 2963 &laquo; prev</a> 3064 </li> 3165 {% endif %} 3266 <li class="page-item"> 3367 <a class="page-link"> 3468 {{rec.number}}/{{rec.paginator.num_pages}}</a> 3569 </li> 3670 {% if rec.has_next %} 3771 <li class="page-item"> 3872 <a class="page-link" href="{% url goto %}{{rec.next_page_number}}"> 3973 next &raquo;</a>

該当のソースコード

python

1urls.py 2urlpatterns = [3path('index_pagination/<int:num>', views.index_pagination, name='index-pagination'),4]5 6views.py 7def index_pagination(request, num=1): 8 template = 'pyapp_sub/index_pagination.html'9 data = Friend.objects.all()10 page = Paginator(data, 3) 11 context = {12 'title': 'index_pagination',13 'ref': 'Refer to views.py def index_pagination',14 'msg': 'Friends.',15 'goto': 'index-pagination',16 'rec': page.get_page(num),17 }18 return render(request, template, context)19

html

1template (body部のみ) 2<body class="container">3 <h1 class="display-4 text-primary">{{ref}}</h1>4 <p class="h4 my-3">{{msg|safe}}</p>5 <table class="table">6 <tr>7 <th>ID</th>8 <th>NAME</th>9 <th>GENDER</th>10 <th>MAIL</th>11 <th>AGE</th>12 <th>BIRTHDAY</th>13 </tr>14 {% for item in rec %} 15 <tr>16 <td>{{item.id}}</td>17 <td>{{item.name}}</td><!--models.py class Friend参照-->18 <td>{% if item.gender == False %}male{% endif %} 19 {% if item.gender == True %}female{% endif %}</td>20 <td>{{item.mail}}</td>21 <td>{{item.age}}</td>22 <td>{{item.birthday}}</td>23 </tr>24 {% endfor %} 25 </table>26 <ul class="pagination justify-content-center">27 {% if rec.has_previous %} 28 <li class="page-item">29 <a class="page-link" href="{% url goto %}">30 &laquo; first</a>31 <!--laquo は <<記号-->32 </li>33 <li class="page-item">34 <a class="page-link" href="{% url goto %}{{rec.previous_page_number}}">35 &laquo; prev</a>36 </li>37 {% else %} 38 <li class="page-item">39 <a class="page-link">40 &laquo; first</a>41 </li>42 <li class="page-item">43 <a class="page-link">44 &laquo; prev</a>45 </li>46 {% endif %} 47 <li class="page-item">48 <a class="page-link">49 {{rec.number}}/{{rec.paginator.num_pages}}</a>50 </li>51 {% if rec.has_next %} <!--これがpreviousであればかろうじて画面出る、リン 52 <li class="page-item"> 53 <a class="page-link" href="{% url goto %}{{rec.next_page_number}}"> 54 next &raquo;</a> 55 <!--raquo は >>記号-->56 </li>57 <li class="page-item">58 <a class="page-link" href="{% url goto %}{{rec.paginator.num_pages}}">59 last &raquo;</a>60 </li>61 {% else %} 62 <li class="page-item">63 <a class="page-link">64 next &raquo;</a>65 </li>66 <li class="page-item">67 <a class="page-link">68 last &raquo;</a>69 </li>70 {% endif %} 71 </ul>72</body>

試したこと・調べたこと

上記の詳細・結果

同様の質問と回答はあるのですが、それに従いコマンド変えても直りません。
Python Django4超入門(掌田津耶乃著)通りのコマンドであるはずであるのに、できない。

補足

特になし

コメントを投稿

0 コメント