Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

python - 分页后调用自定义函数(Call custom function after pagination)

I want to call my function when user clicked on pagination button "Next" or "Previous" on this page http://junjob.ru/

(当用户单击此页面上的分页按钮“下一个”或“上一个”时,我想调用我的函数http://junjob.ru/)

I created solution --- overloaded paginate_queryset method in views.py .

(我创建了解决方案---在views.py中重载了paginate_queryset方法。)

How can I determine when the user clicked “Next” and when “Previous” ?

(如何确定用户何时单击“下一步”以及何时单击“上一步” ?)

views.py

(views.py)

class HomePageView(ListView):
    model = Vacancy
    template_name = 'vacancy_list/vacancy_list.html'
    paginate_by = 5
    page_kwarg = 'vacancy'
    context_object_name = 'vacancies'

    def paginate_queryset(self, queryset, page_size):
        print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx ", self.request.user)
        """Paginate the queryset, if needed."""
        paginator = self.get_paginator(
            queryset, page_size, orphans=self.get_paginate_orphans(),
            allow_empty_first_page=self.get_allow_empty())
        page_kwarg = self.page_kwarg
        page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
        try:
            page_number = int(page)
        except ValueError:
            if page == 'last':
                page_number = paginator.num_pages
            else:
                raise Http404(_('Page is not “last”, nor can it be converted to an int.'))
        try:
            page = paginator.page(page_number)
            return (paginator, page, page.object_list, page.has_other_pages())
        except InvalidPage as e:
            raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
                'page_number': page_number,
                'message': str(e)
            })

vacancy_list.html

(vacancy_list.html)

<!-- Paginator -->
    <div class="container" style="font-size: 2rem;">
    <div class="row justify-content-center">
    <ul class="pagination">
        {% if page_obj.has_previous %}
            <li class="page-item"><a class="page-link" href="?vacancy={{ page_obj.previous_page_number }}">Previous</a></li>
        {% else %}
            <li class="page-item disabled">
                <span class="page-link">Previous</span>
            </li>
        {% endif %}
        <li class="page-item disabled">
                <span class="page-link">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span>
        </li>
        {% if page_obj.has_next %}
           <li class="page-item"><a class="page-link" href="?vacancy={{ page_obj.next_page_number }}">Next</a></li>
        {% else %}
           <li class="page-item disabled">
              <span class="page-link">Next</span>
           </li>
        {% endif %}

    </ul>
    </div>
    </div>
  ask by alcoder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...