我想要所有课程的页码,这很容易实现。但现在我被困了,因为我还想为教师分页,这将显示访问的教师的具体课程。我有四种模式:学院,系,研究和课程。教职员工也会看到分页,但问题是,如果我试图转到第二页,它会将我重定向到所有课程列表的第二页。或者,如果我更改所有课程的页面,然后尝试访问教员,则教员中根本不会显示任何课程。

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">&laquo; 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 &raquo;</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">&laquo; 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 &raquo;</a>
                {% endif %}
            </span>
        </div>
    </div>
{% endfor %}

最佳答案

paginator = Paginator(course_list, 1)
paginator = Paginator(faculty_list, 1)
将第二个实例名称更改为paginator_two可以解决您的问题

10-08 04:03