クラスベースの汎用ビューにページネーションを実装しようとしましたが、私が行った方法では機能しません。
URL
url(r'^cat/(?P<category>[\w+\s]*)/page(?P<page>[0-9]+)/$',
CategorizedPostsView.as_view(), {'paginate_by': 3}),
見る
class CategorizedPostsView(ListView):
template_name = 'categorizedposts.djhtml'
context_object_name = 'post_list'
def get_queryset(self):
cat = unquote(self.kwargs['category'])
category = get_object_or_404(ParentCategory, category=cat)
return category.postpages_set.all()
テンプレート
<div class="pagination">
<span class="step-links">
{% if post_list.has_previous %}
<a href="?page={{ post_list.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ post_list.number }} of {{ post_list.paginator.num_pages }}.
</span>
{% if post_list.has_next %}
<a href="?page={{ post_list.next_page_number }}">next</a>
{% endif %}
</span>
</div>
http:// 127.0.0.1:8000/cat/category_name/?page=1 または http:// 127.0.0.1:8000/cat/category_name/ を取得しようとすると、404 例外が発生しました。
クラスベースのジェネリックビューでページネーションを正しい方法で使用するには?