django分页不显示页码

时间:2014-11-20 08:15:17

标签: django pagination

我的观点:

class HospitalAppointmentView(ListView):
model = DoctorAppointment
template_name = "doctor_appointment_list.html"
paginate_by = 5

def get(self, request, pk, username, hdpk, **kwargs):
    self.pk = pk
    self.username = username
    self.hdpk = hdpk
    return super(HospitalAppointmentView, self).get(request, pk, username, hdpk, **kwargs)

def get_context_data(self, **kwargs):
    context = super(HospitalAppointmentView, self).get_context_data(**kwargs)
    context['appointments'] = DoctorAppointment.objects.filter(hospital__id=self.pk, doctor__id=self.hdpk).order_by("-appointment_date")
    context['today'] = today
    return context

我的模板:

{% for appointment in appointments %}
<table>
<tr>
    <td>{{appointment.appointment_date}}</td>

    <td>{{appointment.first_name}} &nbsp;{{appointment.middle_name}} &nbsp; {{appointment.last_name}}</td>
    <td>{{appointment.user}}</td>

</tr>

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

    <span class="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>

当我这样做时,它会显示页面否,但不会对数据进行分页。它会显示所有列表。它应该只显示5个值,但显示所有值。

提前致谢..

2 个答案:

答案 0 :(得分:0)

约会应该是分页对象。目前您正在发送查询集。

查看https://docs.djangoproject.com/en/dev/topics/pagination/示例。

答案 1 :(得分:0)

您应该覆盖get_queryset,而不是在get_context_data中添加约会。

class HospitalAppointmentView(ListView):
    ...

    def get_queryset(self, **kwargs):
        return DoctorAppointment.objects.filter(
            hospital__id=self.pk,
            doctor__id=self.hdpk).order_by("-appointment_date")

另外,更改您访问object_list的模板变量:

{% for appointment in object_list %}