Giter Site home page Giter Site logo

Comments (4)

JohnPaulGummapu avatar JohnPaulGummapu commented on June 12, 2024

@imran-uk
You're correct that it's not enough to pass page_obj alone to the template

When using Paginator in Django templates, you need to follow these steps.

-> In your view, create an instance of the Paginator class and use it to paginate your queryset.

from django.core.paginator import Paginator

def my_view(request):
    items_per_page = 10  # Define the number of items per page
    queryset = YourModel.objects.all()
    paginator = Paginator(queryset, items_per_page)
    
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    
    results_list = page_obj.object_list  # Extract the list of objects for the current page
    
    context = {
        "page_obj": page_obj,
        "results_list": results_list,
    }
    
    return render(request, 'your_template.html', context)

-> In your template, you can then iterate over the results_list to display the items on the current page.

<ul>
    {% for item in results_list %}
        <li>{{ item.name }}</li>
        <!-- Display other item attributes as needed -->
    {% endfor %}
</ul>

->To create pagination controls, you can use the page_obj provided by the Paginator:

<div class="pagination">
    <span class="step-links">
        {% if page_obj.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ page_obj.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current-page">{{ page_obj.number }}</span>

        {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">next</a>
            <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>

from djangoproject.com.

sathyaar5 avatar sathyaar5 commented on June 12, 2024

Hey @imran-uk that's correct!
It's important to note that you can't simply pass page_obj to the context and expect it to work like in the ListView approach.

Instead, explicitly extract the object_list like this:

page_obj = paginator.get_page(page_number)
results_list = page_obj.object_list

Then, include both "page_obj" and "results_list" in your context:

{
 "page_obj": page_obj,
 "results_list": results_list,
}

In your template, iterate over results_list to display the paginated results. This clarification can save you from the frustration of not seeing results when mistakenly passing just page_obj to your template.

from djangoproject.com.

imran-uk avatar imran-uk commented on June 12, 2024

from djangoproject.com.

jackoconnordev avatar jackoconnordev commented on June 12, 2024

If this issua still exists should a new issue be opened in the main Django repository, since the documentation pages are generated by Sphinx from docs in django/django?

from djangoproject.com.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.