ListView 的分页 - Django

意见

class ThreadListView(ListView):

    model = Thread

    template_name = 'forums/thread.html'


    def get_context_data(self, **kwargs):

        # Call the base implementation first to get a context

        context = super().get_context_data(**kwargs)

        # Add in a QuerySet of the Thread & Replies

        context['thread'] = Thread.objects.get(pk=self.kwargs['pk'])

        context['reply'] = Thread.objects.get(pk=self.kwargs['pk']).replies.all()

        return context

HTML

{% extends 'forums/base.html' %}


{% block title %} Forum - {{ thread.title }} {% endblock title %}


{% block content %}

<!--Thread real-->

<table class="table table-hover">

    <thead>

        <tr class="table-primary">

            <th class="col-2"><a href="{% url 'threadview' thread.id %}"> {{ thread.title }}</a></th>

            <th scope="col-10" id="content-col"></th>

        </tr>

    </thead>


    <tbody>

        <!--Thread Author and Content-->

        <tr class="table-info">

            <td class="border-right text-center">

                <span>

                    <img class="rounded-circle" style="height: 100px;width: 100px;"

                        src="{{ thread.author.profile.image.url }}"> <br />

                    Username:&emsp;<a href="#">{{ thread.author.username|capfirst }}</a> <br />

                    Ranks:&emsp;

                    <!--Ranks Go Here--> <br />

                    <hr>

                    Posts:&emsp;

                    <!--Posts Go Here--> <br />

                    Badges:&emsp;

                    <!--Badges Go Here--> <br />

                    <hr>

                    Date Joined:&emsp;{{thread.author.date_joined| date:'Y-m-d'}} <br />

                </span>

            </td>

{% endblock content %}

我想对线程 ListView 进行分页。


Thread ListView 显示 Thread,然后显示该线程上的回复。


我希望能够将所有内容分成页面。


例如,线程以 Thread 帖子和 10 个回复开始,然后要查看一些较新的回复,您可以单击下一页。


德玛西亚99
浏览 167回答 1
1回答

慕村9548890

将其设为ListViewforReplies并简单地使用paginate_by.class ThreadListView(ListView):&nbsp; &nbsp; model = Reply&nbsp; #change the model&nbsp; &nbsp; template_name = 'forums/thread.html'&nbsp; &nbsp; paginate_by = 10&nbsp; # add pagination on the list&nbsp; &nbsp; def get_queryset(self):&nbsp; &nbsp; &nbsp; &nbsp; self.thread = Thread.objects.get(pk=self.kwargs['pk'])&nbsp; &nbsp; &nbsp; &nbsp; return self.thread.replies.all().order_by('date_posted')&nbsp; &nbsp; def get_context_data(self, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; # Call the base implementation first to get a context&nbsp; &nbsp; &nbsp; &nbsp; context = super().get_context_data(**kwargs)&nbsp; &nbsp; &nbsp; &nbsp; # Add in a QuerySet of the Thread & Replies&nbsp; &nbsp; &nbsp; &nbsp; context['thread'] = self.thread&nbsp; &nbsp; &nbsp; &nbsp; return context然后,在您的模板中,添加分页代码。使用object_list遍历回复...&nbsp; &nbsp; <!--Reply Author and Content-->&nbsp; &nbsp; &nbsp; &nbsp; {% for rply in object_list %}&nbsp;&nbsp;....<div class="container">&nbsp; &nbsp; {% if is_paginated %}&nbsp; &nbsp; {% if page_obj.has_other_pages %}&nbsp; &nbsp; <ul class="pagination justify-content-center" style="margin:20px 0">&nbsp; &nbsp; &nbsp; &nbsp; {% if page_obj.has_previous %}&nbsp; &nbsp; &nbsp; &nbsp; <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>&nbsp; &nbsp; &nbsp; &nbsp; {% else %}&nbsp; &nbsp; &nbsp; &nbsp; <li class="page-item disabled"><a class="page-link" href="#">Previous</a></li>&nbsp; &nbsp; &nbsp; &nbsp; {% endif %}&nbsp; &nbsp; &nbsp; &nbsp; {% for i in page_obj.paginator.page_range %}&nbsp; &nbsp; &nbsp; &nbsp; {% if page_obj.number == i %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="page-item active"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>&nbsp; &nbsp; &nbsp; &nbsp; {% else %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>&nbsp; &nbsp; &nbsp; &nbsp; {% endif %}&nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; &nbsp; &nbsp; {% if page_obj.has_next %}&nbsp; &nbsp; &nbsp; &nbsp; <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>&nbsp; &nbsp; &nbsp; &nbsp; {% else %}&nbsp; &nbsp; &nbsp; &nbsp; <li class="page-item disabled"><a class="page-link" href="#">Next</a></li>&nbsp; &nbsp; &nbsp; &nbsp; {% endif %}&nbsp; &nbsp; </ul>&nbsp; &nbsp; {% endif %}{% endif %}</div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python