我有一个ListView
页面在分页方面运行良好。我还在该页面上添加了Django过滤器以进行过滤。问题是分页工作正常,但是使用Django过滤器后,分页停止工作。页面加载时paginate_by
不起作用。单击分页时,下一页不起作用。
我在做什么错了?
views.py
class JobsListView(ListView, FilterView):
# model = JobPost
queryset = JobPost.objects.all()
template_name = 'jobpost_list.html'
paginate_by = 3
# page_kwargs = 'page'
filterset_class = JobPostFilter
def get_queryset(self, *args, **kwargs):
if self.kwargs:
return JobPost.objects.filter(position=self.kwargs['position']).order_by('-created_at')
else:
query = JobPost.objects.all().order_by('-created_at')
return query
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['todays'] = date.today()
context['filter'] = JobPostFilter(self.request.GET, queryset=self.get_queryset())
return context
模板
{% extends 'base.html' %}
{% load bootstrap4 %}
{% load fontawesome %}
{% block content %}
{% if messages %}
<div class="alert alert-success alert-dismissible fade show">
{% for m in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ m }}</li>
<button type="button" class="close" data-dismiss="alert" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
{% endfor %}
</div>
{% endif %}
<div class="col-12">
<h1 class="text-center">Vacancies</h1>
</div>
<div class="row">
<div class="col-4">
<form method="get">
{% bootstrap_form filter.form %}
{% buttons %}
<button class="btn btn-primary" type="submit">Search {% fontawesome_icon
'search' %}</button>
{% endbuttons %}
</form>
</div>
<div class="col-8">
{% for job in filter.qs %}
<div class="joblists">
<h3><a href='{% url "jobpost_detail" job.slug %}'>{{ job.position }} </a> <a
href="{% url 'jobpost_update_form' job.slug %}">{% fontawesome_icon 'edit' %}
</a></h3>
<p>{{ job.description|safe|truncatewords:30 }}</p>
{% if job.location != '' %}
<p>{% fontawesome_icon 'location-arrow' %} <strong>{{ job.location }}
</strong>
</p>
{% endif %}
<p>{{ job.job_type }}</p>
{% if job.deadline != NULL and job.deadline > todays %}
<p>{% fontawesome_icon 'calendar' %} {{ job.deadline|timeuntil }} <small>to
deadline</small></p>
{% endif %}
{% if job.deadline > todays %}
<p>{% fontawesome_icon 'check-circle' color='green' %} Open</p>
{% elif job.deadline < todays %}
<p>{% fontawesome_icon 'times-circle' color='red' %} Closed</p>
{% endif %}
</div>
{% endfor %}
</div>
<div class="pagination">
{% if is_paginated %}
<hr>
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center pagination-sm">
{% if page_obj.has_previous %}
{% if not search %}
<li class="page-item">
<a class="page-link" href="{% url 'jobpost_list' %}?page={{
page_obj.previous_page_number }}" tabindex="-1">Previous</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{% url 'jobpost_list' %}?{{search}}&page=
{{ page_obj.previous_page_number }}" tabindex="-1">Previous</a>
</li>
{% endif %}
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
{% endif %}
{% for object in page_obj.paginator.page_range %}
<li class="page-item"><a class="page-link" href="{% url 'jobpost_list'
%}?page={{ forloop.counter }}">{{ forloop.counter }}</a></li>
{% endfor %}
{% if page_obj.has_next %}
{% if not search %}
<li class="page-item">
<a class="page-link" href="{% url 'jobpost_list' %}?page={{
page_obj.next_page_number }}">Next</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{% url 'jobpost_list' %}?{{search}}&page=
{{ page_obj.next_page_number }}">Next</a>
</li>
{% endif %}
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#">Next</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
</div>
{% endblock %}
答案 0 :(得分:0)
{% for job in filter.qs %}
中的如果将filter.qs
替换为'object_list',则分页和过滤将起作用。但是,当页面最初加载时未在url中设置任何过滤器参数时,该记录将没有任何记录。
我试图弄清楚该怎么办。正在搜索......