Django 中的分页无法按预期工作
Pagination in Django not working as expected
我想要所有可用课程的分页,这很容易实现。但现在我被困住了,因为我也想为院系分页,这将显示访问的院系的具体课程。我有 4 个模型:院系、部门、研究和课程。分页也会显示给院系,但问题是如果我尝试转到第二页,它会将我重定向到所有课程列表的第二页。或者,如果我更改所有课程的页面,然后尝试访问教员,教员中根本不会显示任何课程。
def index(request):
course_list = Course.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(course_list, 1)
try:
courses = paginator.page(page)
except PageNotAnInteger:
courses = paginator.page(1)
except EmptyPage:
courses = paginator.page(paginator.num_pages)
faculty_list = Faculty.objects.all()
page = request.GET.get('page2', 1)
paginator = Paginator(faculty_list, 1)
try:
faculties = paginator.page(page)
except PageNotAnInteger:
faculties = paginator.page(1)
except EmptyPage:
faculties = paginator.page(paginator.num_pages)
context = {'courses': courses,
'faculties': faculties,
'departments': Department.objects.all(),
'studies': StudyProgramme.objects.all(),
'teachers': Teacher.objects.all()
}
return render(request, 'courses/index.html', context)
<div id="crs">
<h3>All courses</h3>
<ul>
{% for course in courses %}
<li><a href={{ course.slug }}>{{ course.name }}</a></li>
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if courses.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ courses.previous_page_number }}">{{ courses.previous_page_number }}</a>
{% endif %}
<span class="current">
Page {{ courses.number }} of {{ courses.paginator.num_pages }}
</span>
{% if courses.has_next %}
<a href="?page={{ courses.next_page_number }}">{{ courses.next_page_number }}</a>
<a href="?page={{ courses.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
</div>
{% for faculty in faculties %}
<div id="fac_{{ faculty.pk }}_tab" style="display:none;">
<h3> {{ faculty.name }} Courses</h3>
<ul>
{% for department in faculty.department_set.all %}
{% for study in studies %}
{% if study.department == department %}
{% for course in courses %}
{% if course.study_programme == study %}
<li>
<a class="first" href={{ course.slug }}>{{ course.name }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if faculties.has_previous %}
<a href="?page2=1">« first</a>
<a href="?page2={{ faculties.previous_page_number }}">{{ faculties.previous_page_number }}</a>
{% endif %}
<span class="current">
Page {{ faculties.number }} of {{ faculties.paginator.num_pages }}
</span>
{% if faculties.has_next %}
<a href="?page2={{ faculties.next_page_number }}">{{ faculties.next_page_number }}</a>
<a href="?page2={{ faculties.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
</div>
{% endfor %}
这不是 Django 的问题,您的视图肯定会处理一个分页参数 page
,它将指向课程,因此如果您更改每个模型分页的 get 参数,它将起作用。
例如:
## views.py
page = request.GET.get('page')
## yourtemplate.html
<a href="?page={{ yourcontext }}">yourtext</a>
localhost:8000/MYVIEW/?page=2 # Goes to courses
## views.py
page = request.GET.get('faculties')
## yourtemplate.html
<a href="?faculties={{ yourcontext }}">yourtext</a>
##
localhost:8000/MYVIEW/?faculties=2 # Goes to faculties
等..
已编辑:
{% for faculty in faculties %} 将其更改为 {% for faculty in faculties2 %} 并从您的上下文中删除 faculties,因为您在重复自己,因为 faculties2 和 faculties 持有相同的查询集。
paginator = Paginator(course_list, 1)
paginator = Paginator(faculty_list, 1)
将您的第二个实例名称更改为 paginator_two
将解决您的问题
我想要所有可用课程的分页,这很容易实现。但现在我被困住了,因为我也想为院系分页,这将显示访问的院系的具体课程。我有 4 个模型:院系、部门、研究和课程。分页也会显示给院系,但问题是如果我尝试转到第二页,它会将我重定向到所有课程列表的第二页。或者,如果我更改所有课程的页面,然后尝试访问教员,教员中根本不会显示任何课程。
def index(request):
course_list = Course.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(course_list, 1)
try:
courses = paginator.page(page)
except PageNotAnInteger:
courses = paginator.page(1)
except EmptyPage:
courses = paginator.page(paginator.num_pages)
faculty_list = Faculty.objects.all()
page = request.GET.get('page2', 1)
paginator = Paginator(faculty_list, 1)
try:
faculties = paginator.page(page)
except PageNotAnInteger:
faculties = paginator.page(1)
except EmptyPage:
faculties = paginator.page(paginator.num_pages)
context = {'courses': courses,
'faculties': faculties,
'departments': Department.objects.all(),
'studies': StudyProgramme.objects.all(),
'teachers': Teacher.objects.all()
}
return render(request, 'courses/index.html', context)
<div id="crs">
<h3>All courses</h3>
<ul>
{% for course in courses %}
<li><a href={{ course.slug }}>{{ course.name }}</a></li>
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if courses.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ courses.previous_page_number }}">{{ courses.previous_page_number }}</a>
{% endif %}
<span class="current">
Page {{ courses.number }} of {{ courses.paginator.num_pages }}
</span>
{% if courses.has_next %}
<a href="?page={{ courses.next_page_number }}">{{ courses.next_page_number }}</a>
<a href="?page={{ courses.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
</div>
{% for faculty in faculties %}
<div id="fac_{{ faculty.pk }}_tab" style="display:none;">
<h3> {{ faculty.name }} Courses</h3>
<ul>
{% for department in faculty.department_set.all %}
{% for study in studies %}
{% if study.department == department %}
{% for course in courses %}
{% if course.study_programme == study %}
<li>
<a class="first" href={{ course.slug }}>{{ course.name }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if faculties.has_previous %}
<a href="?page2=1">« first</a>
<a href="?page2={{ faculties.previous_page_number }}">{{ faculties.previous_page_number }}</a>
{% endif %}
<span class="current">
Page {{ faculties.number }} of {{ faculties.paginator.num_pages }}
</span>
{% if faculties.has_next %}
<a href="?page2={{ faculties.next_page_number }}">{{ faculties.next_page_number }}</a>
<a href="?page2={{ faculties.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
</div>
{% endfor %}
这不是 Django 的问题,您的视图肯定会处理一个分页参数 page
,它将指向课程,因此如果您更改每个模型分页的 get 参数,它将起作用。
例如:
## views.py
page = request.GET.get('page')
## yourtemplate.html
<a href="?page={{ yourcontext }}">yourtext</a>
localhost:8000/MYVIEW/?page=2 # Goes to courses
## views.py
page = request.GET.get('faculties')
## yourtemplate.html
<a href="?faculties={{ yourcontext }}">yourtext</a>
##
localhost:8000/MYVIEW/?faculties=2 # Goes to faculties
等..
已编辑:
{% for faculty in faculties %} 将其更改为 {% for faculty in faculties2 %} 并从您的上下文中删除 faculties,因为您在重复自己,因为 faculties2 和 faculties 持有相同的查询集。
paginator = Paginator(course_list, 1)
paginator = Paginator(faculty_list, 1)
将您的第二个实例名称更改为 paginator_two
将解决您的问题