Django ListView中的Paginator不适用于get函数

时间:2019-06-18 16:25:12

标签: django listview paginator

Django ListView中的

Paginator不适用于get函数。前进和后退按钮出现在html页面上,但绝不会将数据划分为页面。

还尝试使用page_obj而不是page

views.py

class LessonListView(ListView):
    model = Lesson
    template_name = 'edms/lesson/list.html'
    success_url = '/lessons'
    paginate_by = 10


    def get(self, request):
        if request.user.is_authenticated:
            user = request.user
            if user.is_department_manager or             user.is_assistant_department_manager or user.is_dean_manager:
                documnets = Requested_Documents.objects.filter(
                    lesson__user=user
                ).order_by("lesson")
                lessons = {}
                all_lessons = Lesson.objects.all()
                paginator = Paginator(all_lessons, 5) 
                page = request.GET.get('page')       
                for lesson in all_lessons:
                    lesson_id = str(lesson.id)
                    lessons[lesson_id] = {
                            'lesson': lesson,
                            'docs': []
                        }
                for doc in documnets:
                    lesson_id = str(doc.lesson.id)
                    lessons[lesson_id]['docs'].append(doc)
                return render(request, self.template_name, {
                    'lessons': lessons,
                    'pages' : pages
                })
            elif user.is_academician:
                documnets = Requested_Documents.objects.filter(
                    lesson__user=user
                ).order_by("lesson")
                lessons = {}
                for lesson in Lesson.objects.filter(user=user):
                    lesson_id = str(lesson.id)
                    lessons[lesson_id] = {
                            'lesson': lesson,
                            'docs': []
                        }
                for doc in documnets:
                    lesson_id = str(doc.lesson.id)
                    lessons[lesson_id]['docs'].append(doc)
                return render(request, self.template_name, {
                    'lessons': lessons,
                })
        else:
            return redirect(reverse('edms:login'))

list.html

<div class ="pagination">
    <span class="step-links">
        {% if pages.has_previous %}
            <a href="?page={{ pages.previous_page_number }}">Previous</a>
        {% endif %}
        <span class="current">
            Page {{ pages.number }} of {{ pages.paginator.num_page_obj }}.
        </span>
        {% if pages.has_next %}
            <a href="?page={{ pages.next_page_number }}">Next</a>
        {% endif %}
    </span>
</div>

输出 页面底部列出和编写的所有课程

0 个答案:

没有答案