我的观点:
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}} {{appointment.middle_name}} {{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个值,但显示所有值。
提前致谢..
答案 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 %}