2

I wanted pagination for all the courses available and that was easy to achieve. But now I'm stuck because I wanted pagination for faculties also, which will show specific courses of the accessed faculty. I have 4 models: faculties, departments, studies, and courses. The pagination will show for faculties as well, but the problem is that if I try to go to the second page, it will redirect me to the second page of all courses list. Or, if I change page on all courses and then try to access a faculty, no course will show at all in the faculty.

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 %}
i_am_deesh
  • 448
  • 3
  • 12

2 Answers2

2

It's not a Django problem that your view is most certainly handle one pagination parameter page which will direct to courses, so if you changed the get parameter for each of your model pagination it will work.

ex:

## 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

etc..

edited:

{% for faculty in faculties %} change it to {% for faculty in faculties2 %} and remove faculties from your context, cuz you are repeating yourself as faculties2 and faculties hold the same queryset .

misraX
  • 887
  • 5
  • 15
  • I tried something like that. I was unsuccessful. Please look here: https://pastebin.com/mk1QTfd7 –  Feb 13 '18 at 18:56
  • Please share your snippet in the question itself, as it will help others. – misraX Feb 13 '18 at 19:03
  • Your contexts aren't right and your templates call to the pagination follow the same way, so when you call the pagination object it will will not return a result as you did not use it in your queryset for loop. – misraX Feb 13 '18 at 19:09
  • {% for faculty in faculties %} change it to {% for faculty in faculties2 %} and remove faculties from your context, cuz you are repeating yourself as faculties2 and faculties hold the same queryset but faculties doesn't has the pagination object which will not go to the next page. – misraX Feb 13 '18 at 19:14
  • I reedited the above snippet. Also, if I do what you just mentioned now.. nothing works properly anymore. –  Feb 13 '18 at 19:16
  • I edited the post again. Now, if I press a page in all courses, if that page is number 1, object one from faculty model is shown, or i press page 2 object two from faculty model is shown and so on. And then if I press on the faculty that just appeared, it will display its specific pagination with course, but if I try to go to second page for example, it will take me to all courses again.. –  Feb 13 '18 at 19:30
  • Did you change your context as I mentioned – misraX Feb 13 '18 at 19:33
  • and your template too. – misraX Feb 13 '18 at 19:33
1

paginator = Paginator(course_list, 1)

paginator = Paginator(faculty_list, 1)

Changing your second instance name to paginator_two would solve your problem

Işık Kaplan
  • 2,815
  • 2
  • 13
  • 28