Question

I am using django-pagination to paginate the a list of objects in my temlate. I have installed the app, added it in my project and added pagination.middleware.PaginationMiddleware in my settings.py file. But when I try to use it in my template the object_list is not being paginated. Here is my template code

{% extends "base.html" %}

    {% load pagination_tags %}
    {% autopaginate Questions %}

    {% block title %}
        Questions
    {% endblock %}


    {% block content %}
        <div id="contentDiv">

            {% for question in Questions %}
                <div style="padding:5px 20px 5px 30px;">

                    <p class='question'><span id='style2'>Q
                        <a href="{{ question.get_absolute_url }}" style="text-decoration:none; color:black;"> </span> {{ question.questiontext|safe }} </a> 
                        <span style= 'float:right;'><span style='font-size:12px; color:#099;'><a href="/question/type={{question.type}}" 
                        style='font-size:12px; color:#099;'>{{question.type}}</a></span> <span style='color:#99C; font-size:12px;'>Level: </span><span style='color:#099;font-size:12px;'>{{question.level}}</a></span></span>
                    </p>

                    <h2 class='trigger1' ><a href='#'>Answer</a></h2>
                    <div class='toggle_container' >
                        <div class='block' style='background-color:#fff; '>
                            <p class='ans'> {{ question.answer|safe }} </p>
                        </div>
                    </div>

                </div>
            {% endfor %}

            <div class="pagination" style="width:1000px; margin:auto; margin-bottom:20px;">
                {% paginate %}
            </div>

        </div>

{% endblock %}

The list of objects is in the context_variable called Questions. Am I doing something wrong?

Était-ce utile?

La solution

After a very long time I have been able to find out the error I was having with django-pagination. I had the canonical base template which I was extending on all pages.

In the documentation it is written that we require to put {% paginate %} after {% autopaginate object_list %} but no where it was written about the placement of {% autopaginate object_list %} itself.

I had title and body blocks in my template, and I was putting {% autopaginate object_list %} just below the {% extends "base.html" %} and as a result it was not working. I found that I had to put this statement inside the body block and now it is working absolutely fine.

Autres conseils

Can you see the content of your pagination div if you write "Hello, I want a burger" or anything else in there?

Are you sure you have enough Questions to paginate? You could try something like:

    {% autopaginate Questions 2 %}

to make sure that you'll be paginating at 2 questions/page.

Solved as Sachin told above: I just moved {% load pagination_tags %}{% autopaginate list_objs 10 %} inside {% block content %} statement (previously it was outside of it, so pagination was invisible. If no errors, but now pages - try to play with it (moving pagination block).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top