分页不显示ListView中的页码

时间:2016-04-26 02:48:23

标签: python django

我能够在基于函数的视图中使分页工作正常,但现在我将所有内容重新分解为基于类的视图,现在分页数字没有显示。

views.py

from django.shortcuts import render, get_object_or_404
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.contrib.messages.views import SuccessMessageMixin
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from .forms import ProfileForm
from .models import EmployeeProfiles


class IndexView(generic.ListView):
    template_name = 'ppm/ppm.html'
    paginate_by = 5  # Show 5 profiles per page

    def get(self, request):
        profiles_set = EmployeeProfiles.objects.all()
        paginator = Paginator(profiles_set, self.paginate_by) 
        page = request.GET.get('page')
        try:
            profiles = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            profiles = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            profiles = paginator.page(paginator.num_pages)

        context = {
            'profiles_set': profiles_set,
            'title': 'Employee Profiles'
         }

    return render(request, self.template_name, context)

ppm.html

<div class="pagination">
    <span class="step-links">
        {% if profiles_set.has_previous %}
            <a href="?page={{ profiles_set.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ profiles_set.number }} of {{ profiles_set.paginator.num_pages }}.
        </span>

        {% if profiles_set.has_next %}
            <a href="?page={{ profiles_set.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

输出&#39;页面。 &#39;

我已经查看了类似问题的解决方案,并尝试了这些解决方案,但仍然无法找到解决方案。我有什么想法吗?

1 个答案:

答案 0 :(得分:2)

首先,您不需要覆盖get。您的代码足够简单,可用于默认实现。

<强> views.py

class IndexView(ListView):
    model = EmployeeProfiles
    paginate_by = 5
    template_name = 'ppm/ppm.html'

其次,在CBW提供的模板使用分页中。

<强> ppm.html

<table>
{% for profile in object_list %}
<tr>
    <td>{{ profile.name }}</td>
</tr>
{% endfor %}
</table>

<p>
{% if is_paginated %}
<nav>
    <ul class="pagination">
    {% if page_obj.has_previous %}
        <li>
        <a href="?page={{ page_obj.previous_page_number }}">
            <span>Previous</span>
        </a>
        </li>
    {% else %}
        <li class="disabled">
        <a href="#">
            <span>Previous</span>
        </a>
        </li>
    {% endif %}

    {% for page in paginator.page_range %}
        <li {% if page == page_obj.number %}class="active"{% endif %}>
        <a href="?page={{ page }}">{{ page }}</a>
        </li>
    {% endfor %}

    {% if page_obj.has_next %}
        <li>
        <a href="?page={{ page_obj.next_page_number }}">
            <span>Next</span>
        </a>
        </li>
    {% else %}
        <li {% if not page_obj.has_next %}class="disabled"{% endif %}>
        <a href="#">
            <span>Next</span>
        </a>
        </li>
    {% endif %}
    </ul>
</nav>
{% endif %}
</p>

并提出一些建议:最好将单数用于模型名称。