我现在正在努力解决这个问题,我接近了一些事情。我按以下顺序通过外键将四个模型相互绑定:Faculty>部门> StudyProgramme>课程。我创建了搜索所有课程的分页。现在我想为仅属于特定教师的课程创建分页。例如,我总共有3门课程,其中2门课程属于信息学系,1门课程属于医学系,每门课程需要1门课程。 经过多次尝试,我想过使用院系id作为个人分页的方式,但没有成功。所有课程的教师都会显示,而不是只有1,如果我点击下一步,我会得到重定向到所有课程的分页。我不知道如何解决这个问题:
@login_required
def index(request):
query_list = Course.objects.all()
query = request.GET.get('q')
if query:
query_list = query_list.filter(Q(name__icontains=query))
paginator = Paginator(query_list, 1)
page = request.GET.get('page')
faculty_id = request.GET.get('faculty_id')
courses_to_display = None
try:
courses = paginator.page(page)
except PageNotAnInteger:
courses = paginator.page(1)
except EmptyPage:
courses = paginator.page(paginator.num_pages)
if not faculty_id:
courses_to_display = courses
faculties = Faculty.objects.all()
for faculty in faculties:
f_courses = []
departments = faculty.department_set.all()
for department in departments:
studies = department.studyprogramme_set.all()
for study in studies:
for course in study.course_set.all():
f_courses.append(course)
paginated_courses = Paginator(f_courses, 1)
try:
fp_courses = paginated_courses.page(page)
except PageNotAnInteger:
fp_courses = paginated_courses.page(1)
except EmptyPage:
fp_courses = paginated_courses.page(paginated_courses.num_pages)
faculty.paginated_courses = fp_courses
if faculty_id:
courses_to_display = fp_courses
context = {
'courses': courses_to_display,
'faculties': faculties,
'departments': Department.objects.all(),
'studies': StudyProgramme.objects.all(),
'teachers': Teacher.objects.all()
}
return render(request, 'courses/index.html', context)

<div class="col-md-9">
<div class="jumbotron">
<div id="sup">
<h4>All Courses</h4>
<hr>
<br>
<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 }}">previous</a>
{% endif %}
<span class="current">
{{ courses.number }}
</span>
{% if courses.has_next %}
<a href="?page={{ courses.next_page_number }}">next</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 department.studyprogramme_set.all %}
{% for course in study.course_set.all %}
<li>
<a class="first"
href={{ course.slug }}>{{ course.name }}</a>
</li>
{% endfor %}
{% endfor %}
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if faculty.paginated_courses.has_previous %}
<a href="?page=1&faculty_id={{ faculty.id }} ">« first</a>
<a href="?page={{ faculty.paginated_courses.previous_page_number }}&faculty_id={{ faculty.id }}">previous</a>
{% endif %}
<span class="current">
{{ faculty.paginated_courses.number }}
</span>
{% if faculty.paginated_courses.has_next %}
<a href="?page={{ faculty.paginated_courses.next_page_number }}&faculty_id={{ faculty.id }}">next</a>
<a href="?page={{ faculty.paginated_courses.paginator.num_pages }}&faculty_id={{ faculty.id }}">last »</a>
{% endif %}
</span>
</div>
</div>
{% endfor %}
</div>
&#13;