Django 过滤器、搜索查询和分页
Django Filter, seacrh query and pagination
我在使用过滤器和分页时遇到问题,我已经尝试了很多我在此处看到的有关堆栈溢出的示例,但似乎没有任何效果。我正在使用 CBV(ListView)。过滤器有效,但分页有问题,当我单击下一步或第 2 页时,过滤器会中断。我希望页面分页并在下一页显示剩余的过滤项目。
template.html
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-info mb-4" href="?{% if object_list %}q={{ object_list }}&{% endif %}page={{ page_obj.previous_page_number }}">« Previous</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="btn btn-info mb-4" href="?{% if object_list %}q={{ filter }}&{% endif %}page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-info mb-4" href="?{% if object_list %}q={{ object_list }}&{% endif %}page={{ page_obj.next_page_number }}">Next</a>
<a class="btn btn-info mb-4" href="?{% if object_list %}q={{ object_list }}&{% endif %}page={{ page_obj.paginator.num_pages }}">Last «</a>
{% endif %}
{% endif %}
然后我使用了 Django-filters 但过滤器坏了。我将如何最好地实现我正在寻找的东西。
models.py
class Job(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
filters.py
class SearchFilter(django_filters.FilterSet):
title = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Job
fields = ('title',)
views.py
class SearchView(FilterView):
model = Job
template_name = 'search.html'
filterset_class = SearchFilter
ordering = ['-date']
paginate_by = 3
context_object_name = 'jobs'
strict = False
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['filter'] = SearchFilter(self.request.GET, queryset=self.get_queryset())
return context
将此添加到您的 get_context_data 中:
query = self.request.GET.copy()
if 'page' in query:
del query['page']
context['queries'] = query
这是一个完整的列表视图分页:
{% if is_paginated %}
{% if page_obj.has_previous %}
<li>
<a href="?{{ queries.urlencode }}&page=1"><i class="fa fa-angle-double-left"></i></a>
</li>
<li>
<a href="?{{ queries.urlencode }}&page={{ page_obj.previous_page_number }}"><i class="fa fa-angle-left"></i></a>
</li>
{% else %}
<li>
<a href="#"><i class="fa fa-angle-double-left"></i></a>
</li>
<li>
<a href="#"><i class="fa fa-angle-left"></i></a>
</li>
{% endif %}
{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li>
<a href="#">{{ i }}</a>
</li>
{% elif i > page_obj.number|add:'-4' and i < page_obj.number|add:'4' %}
<li>
<a href="?{{ queries.urlencode }}&page={{ i }}">{{ i }}</a>
</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>
<a href="?{{ queries.urlencode }}&page={{ page_obj.next_page_number }}"><i class="fa fa-angle-right"></i></a>
</li>
<li>
<a href="?{{ queries.urlencode }}&page={{ page_obj.paginator.num_pages }}"><i class="fa fa-angle-double-right"></i></a>
</li>
{% else %}
<li>
<a href="#"><i class="fa fa-angle-right"></i></a>
</li>
<li>
<a href="#"><i class="fa fa-angle-double-right"></i></a>
</li>
{% endif %}
{% endif %}
我在使用过滤器和分页时遇到问题,我已经尝试了很多我在此处看到的有关堆栈溢出的示例,但似乎没有任何效果。我正在使用 CBV(ListView)。过滤器有效,但分页有问题,当我单击下一步或第 2 页时,过滤器会中断。我希望页面分页并在下一页显示剩余的过滤项目。
template.html
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-info mb-4" href="?{% if object_list %}q={{ object_list }}&{% endif %}page={{ page_obj.previous_page_number }}">« Previous</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="btn btn-info mb-4" href="?{% if object_list %}q={{ filter }}&{% endif %}page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-info mb-4" href="?{% if object_list %}q={{ object_list }}&{% endif %}page={{ page_obj.next_page_number }}">Next</a>
<a class="btn btn-info mb-4" href="?{% if object_list %}q={{ object_list }}&{% endif %}page={{ page_obj.paginator.num_pages }}">Last «</a>
{% endif %}
{% endif %}
然后我使用了 Django-filters 但过滤器坏了。我将如何最好地实现我正在寻找的东西。
models.py
class Job(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
filters.py
class SearchFilter(django_filters.FilterSet):
title = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Job
fields = ('title',)
views.py
class SearchView(FilterView):
model = Job
template_name = 'search.html'
filterset_class = SearchFilter
ordering = ['-date']
paginate_by = 3
context_object_name = 'jobs'
strict = False
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['filter'] = SearchFilter(self.request.GET, queryset=self.get_queryset())
return context
将此添加到您的 get_context_data 中:
query = self.request.GET.copy()
if 'page' in query:
del query['page']
context['queries'] = query
这是一个完整的列表视图分页:
{% if is_paginated %}
{% if page_obj.has_previous %}
<li>
<a href="?{{ queries.urlencode }}&page=1"><i class="fa fa-angle-double-left"></i></a>
</li>
<li>
<a href="?{{ queries.urlencode }}&page={{ page_obj.previous_page_number }}"><i class="fa fa-angle-left"></i></a>
</li>
{% else %}
<li>
<a href="#"><i class="fa fa-angle-double-left"></i></a>
</li>
<li>
<a href="#"><i class="fa fa-angle-left"></i></a>
</li>
{% endif %}
{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li>
<a href="#">{{ i }}</a>
</li>
{% elif i > page_obj.number|add:'-4' and i < page_obj.number|add:'4' %}
<li>
<a href="?{{ queries.urlencode }}&page={{ i }}">{{ i }}</a>
</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>
<a href="?{{ queries.urlencode }}&page={{ page_obj.next_page_number }}"><i class="fa fa-angle-right"></i></a>
</li>
<li>
<a href="?{{ queries.urlencode }}&page={{ page_obj.paginator.num_pages }}"><i class="fa fa-angle-double-right"></i></a>
</li>
{% else %}
<li>
<a href="#"><i class="fa fa-angle-right"></i></a>
</li>
<li>
<a href="#"><i class="fa fa-angle-double-right"></i></a>
</li>
{% endif %}
{% endif %}