我希望所有可用课程都有一个分页,这很容易实现。但是现在我被卡住了,因为我也希望对院系进行分页,这将显示所访问的教师的特定课程。我有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 %}
答案 0 :(得分:3)
不是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%}并从您的上下文中移除院系,因为你重复自己作为faculties2和院系持有相同的查询集。
答案 1 :(得分:1)
paginator = Paginator(course_list, 1)
paginator = Paginator(faculty_list, 1)
将您的第二个实例名称更改为paginator_two
可以解决您的问题