Вопрос

I working on pagination with ListView with django. Everything is working as expected. But there is no next and previous links on template.

Am i missing something here ?

Here is urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic.list import ListView
from demoapp.models import Candidate
admin.autodiscover()

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),
    url(r'^test/', ListView.as_view(
                    model=Candidate,
                    paginate_by='10',
                    queryset=Candidate.objects.all(),
                    context_object_name="tasks",                
                    template_name='index.html')),
)

models.py

from django.db import models
class Candidate(models.Model):
    name=models.CharField(max_length=255)

And here is index.html

<ol>
{% for t in tasks %}
<li><p>{{t.name}}</p></li>
{% endfor %}
</ol>
<div class="pagination">
      <ul>
          {% if paginator.has_previous %}
              <li><a href="?page={{ paginator.previous_page_number }}">Previous</a></li>
          {% endif %}
          {% for pg in paginator.page_range %}
              {% if posts.number == pg %}
                  <li class="active"><a href="?page={{ pg }}">{{ pg }}</a></li>
              {% else %}
                  <li><a href="?page={{ pg }}">{{ pg }}</a></li>
              {% endif %}
          {% endfor %}
          {% if paginator.has_next %}
              <li><a href="?page={{ paginator.next_page_number }}">Next</a></li>
          {% endif %}
      </ul>
    </div>
Это было полезно?

Решение

Nothing shows up because Django doesn't know what paginator is, in the view template.

Looking at the example in the documentation, it seems you need to replace paginator in the view with tasks -

<div class="pagination">
  <ul>
      {% if tasks.has_previous %}
          <li><a href="?page={{ tasks.previous_page_number }}">Previous</a></li>
      {% endif %}
      {% for pg in tasks.page_range %}
          {% if posts.number == pg %}
              <li class="active"><a href="?page={{ pg }}">{{ pg }}</a></li>
          {% else %}
              <li><a href="?page={{ pg }}">{{ pg }}</a></li>
          {% endif %}
      {% endfor %}
      {% if tasks.has_next %}
          <li><a href="?page={{ tasks.next_page_number }}">Next</a></li>
      {% endif %}
  </ul>
</div>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top