0

Views

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>

            <td>{{ thread.content }}</td>
        </tr>
        <!--Reply Author and Content-->
        {% for rply in reply %}
        <tr class="table-secondary">
            <td class="border-right text-center">
                <span>
                    <img class="rounded-circle" style="height: 100px;width: 100px;"
                        src="{{ rply.author.profile.image.url }}"> <br />
                    Username:&emsp;<a href="#">{{ rply.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>
            <td>
                <p>{{ rply.content }}</p>
            </td>
        </tr>
        {% endfor %}

    </tbody>
</table>

{% endblock content %}

I want to Paginate the Thread ListView.

The Thread ListView shows the Thread and then it shows the replies that are on that thread.

I want to be able to split up all the content into pages.

An example is that the thread starts with the Thread post and 10 replies and then to view some of the newer replies you will be able to click on the next page.

José Ricardo Pla
  • 1,043
  • 10
  • 16
Bryant
  • 421
  • 2
  • 10

1 Answers1

1

Make this the ListView for Replies and simply use paginate_by.

class ThreadListView(ListView):
    model = Reply  #change the model
    template_name = 'forums/thread.html'
    paginate_by = 10  # add pagination on the list

    def get_queryset(self):
        self.thread = Thread.objects.get(pk=self.kwargs['pk'])
        return self.thread.replies.all().order_by('date_posted')


    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'] = self.thread
        return context

Then, in you template, add the code for pagination. Use object_list to iterate over replies

...
    <!--Reply Author and Content-->
        {% for rply in object_list %}  
....


<div class="container">
    {% if is_paginated %}
    {% if page_obj.has_other_pages %}
    <ul class="pagination justify-content-center" style="margin:20px 0">
        {% if page_obj.has_previous %}
        <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
        {% else %}
        <li class="page-item disabled"><a class="page-link" href="#">Previous</a></li>
        {% endif %}
        {% for i in page_obj.paginator.page_range %}
        {% if page_obj.number == i %}
            <li class="page-item active"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
        {% else %}
            <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
        {% endif %}
        {% endfor %}
        {% if page_obj.has_next %}
        <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
        {% else %}
        <li class="page-item disabled"><a class="page-link" href="#">Next</a></li>
        {% endif %}
    </ul>
    {% endif %}
{% endif %}
</div>
Bryant
  • 421
  • 2
  • 10
Aman Garg
  • 2,507
  • 1
  • 11
  • 21
  • Just tried it. It shows the pagination links. But it doesn't change the content when pages are changed. I changed the paginate_by to = 3 because i have 4 replies setup and one thread. But when i am on page one or page two it shows all four replies. – Bryant Apr 23 '19 at 04:44
  • updated the answer.. use `object_list` to iterate over replies – Aman Garg Apr 23 '19 at 06:13
  • Thanks it works. I had to add the ordering to the end of the replies.all(). I am gonna edit your answer to add it in. – Bryant Apr 23 '19 at 07:13