سؤال

My code looks like this answer but I am stuck with URLconf. I have an app installed and the pagination should be on index view. Current regex for it is:

url(r'^$', views.IndexView.as_view(), name='index')
(line from urls.py)

I assume my pagination doesn't work because of it since I always get same results no matter which page. I hope I was clear and straight to point so you give me an answer fast, thanks! :)

views.py

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    paginate_by = 3
    queryset = models.Poll.objects.all()

urls.py

urlpatterns = patterns('',
     url(r'^$', views.IndexView.as_view(), name='index'),
     url(r'^(?P<pk>\d+)/$', views.PollView.as_view(), name='poll'),
)

index.html

{% if poll_list %}
<ul>
    {% for poll in poll_list %}
    <li><a href="{% url 'polls:poll' poll.id %}">{{ poll.p_date }}</a></li>
        <p>{{ poll.p_content }}</p>
    </li>
    {% endfor %}
</ul>

{% if is_paginated %}
    <div class="pagination">
        <span class="page-links">
            {% if page_obj.has_previous %}
            <a href="?p={{ page_obj.previous_page_number }}">previous</a>
            {% endif %}
            <span class="page-current">
                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
            </span>
            {% if page_obj.has_next %}
            <a href="?p={{ page_obj.next_page_number }}">next</a>
            {% endif %}
        </span>
    </div>
{% endif %}


{% else %}
    <p>No polls.</p>
{% endif %}
هل كانت مفيدة؟

المحلول

If I recall correctly, the pagination's GET variable is called page, not p (as you use in your template. Try changing it to this:

<span class="page-links">
    {% if page_obj.has_previous %}
    <a href="?page={{ page_obj.previous_page_number }}">previous</a>
    {% endif %}
    <span class="page-current">
        Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
    </span>
    {% if page_obj.has_next %}
    <a href="?page={{ page_obj.next_page_number }}">next</a>
    {% endif %}
</span>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top