Home > Blockchain >  Pagination doesn`t work with extra context in Django ListView
Pagination doesn`t work with extra context in Django ListView

Time:11-26

I am trying to create a simple pagination through a query of filtered instances of Need model. The problem is that pagination doesn`t work at all, and I guess this is because of extra content. Here is my current view, which shows pagination on front-end as an empty div:

class CategoryNeeds(ListView):
    model = Need
    template_name = "volunteering/category_needs.html"
    paginate_by = 1
    context_object_name = "needs"

    def get_queryset(self):
        return Need.objects.filter(category__name=self.kwargs["name"].capitalize())

    def get(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        category = Category.objects.get(name=kwargs["name"].capitalize())

        self.extra_context = {
            "category": category,
            "needs": self.object_list
        }

        return self.render_to_response(self.extra_context)

And here is the template:

{% extends "index/index.html" %}
{% block content %}
    <section >
        <div >
            <div >
                <div >
                    <div >
                        <h1>{{ category.name }}</h1>
                        <hr style="size: A5">
                    </div>
                </div>
            </div>
        </div>
    </section>
    <section  id="about">
        <div >
            <h2>Needs:</h2>
            <table >
                <thead>
                <tr>
                    <th scope="col">Photo</th>
                    <th scope="col">Title</th>
                    <th scope="col">Description</th>
                    <th scope="col">Price</th>
                    <th scope="col">Donation</th>
                    <th scope="col">City</th>
                    {% if user.pk == user_page.pk %}
                        <th scope="col">Update</th>
                        <th scope="col">Delete</th>
                    {% endif %}
                </tr>
                </thead>
                <tbody>
                {% for need in needs %}
                    <tr data-href="{% url "volunteering:need" need.pk %}">
                        <td>{% if need.photo %}<img src="{{ need.photo.url }}">{% endif %}</td>
                        <td>{{ need.title }}</td>
                        <td>{{ need.description|truncatechars:10 }}</td>
                        <td>{{ need.price }}</td>
                        <td>{{ need.donation }}</td>
                        <td>{{ need.city }}</td>
                        {% if user.pk == user_page.pk %}
                            <td><a href="{% url "volunteering:update_need" need.pk %}">Update</a></td>
                            <td><a href="{% url "volunteering:delete_need" need.pk %}">Delete</a></td>
                        {% endif %}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div>
            {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}">« Previous page</a>
                {% if page_obj.number > 3 %}
                    <a href="?page=1">1</a>
                    {% if page_obj.number > 4 %}
                        <span>...</span>
                    {% endif %}
                {% endif %}
            {% endif %}
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                    <a href="?page={{ num }}">{{ num }}</a>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <a href="?page={{ num }}">{{ num }}</a>
                {% endif %}
            {% endfor %}
            {% if page_obj.has_next %}
                {% if page_obj.number < page_obj.paginator.num_pages|add:'-3' %}
                    <span>...</span>
                    <a href="?page={{ page_obj.paginator.num_pages }}">{{ page_obj.paginator.num_pages }}</a>
                {% elif page_obj.number < page_obj.paginator.num_pages|add:'-2' %}
                    <a href="?page={{ page_obj.paginator.num_pages }}">{{ page_obj.paginator.num_pages }}</a>
                {% endif %}
                <a href="?page={{ page_obj.next_page_number }}">Next Page »</a>
            {% endif %}
        </div>
    </section>
{% endblock %}

The reason why I think the problem lies in extra content is because this view works perfectly with pagination:

class AllNeeds(ListView):
    model = Need
    context_object_name = "needs"
    paginate_by = 3

Could someone explain, why doesn`t my pagination work, please? Would be very grateful for all your responce!

CodePudding user response:

Yep, it seems that you are overiding normal django flow which adds context, try this:

class CategoryNeeds(ListView):
    model = Need
    template_name = "volunteering/category_needs.html"
    paginate_by = 1

    def get_queryset(self):
        return Need.objects.filter(category__name=self.kwargs["name"].capitalize())

    def get(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        category = Category.objects.get(name=kwargs["name"].capitalize())

        context = self.get_context_data()
        context['category'] = category

        return self.render_to_response(context)
  • Related