1

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?

Sachin
  • 3,672
  • 9
  • 55
  • 96

3 Answers3

2

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.

Sachin
  • 3,672
  • 9
  • 55
  • 96
0

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).

mishaikon
  • 461
  • 5
  • 12
0

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.

scoopseven
  • 1,767
  • 3
  • 18
  • 27
  • Yes I can see the whole content and I have enough questions per page... The problem is that it not paginating which means it is showing all the pages as it is... I have around 100 questions and they all are coming on the same page – Sachin Dec 28 '11 at 11:11