我正在使用Django-filter对列表视图查询集进行过滤和排序。
当前统计信息-用户提交过滤器时,页面会刷新并显示新查询。
我的目标-在无需刷新页面的情况下获取列表更新+在每个过滤器选择中都进行列表更新(而不是等待用户选择所有所需的过滤器并提交)
我相信该解决方案将包括Ajax,但是我不确定如何通过Django过滤器来使用它,不幸的是,文档中没有任何提及。
Filter.py
class FestivalFilter(django_filters.FilterSet):
style = django_filters.MultipleChoiceFilter(choices=Festival.style_choices,widget=forms.CheckboxSelectMultiple,)
camp_style = django_filters.MultipleChoiceFilter(choices=Festival.camp_style_choices,widget=forms.CheckboxSelectMultiple,)
friendly = django_filters.BooleanFilter(method='avg_above3',)
.....
def avg_above3(self, queryset, name, value):
return queryset.annotate(avg=Avg('Festival_Reviews__{}'.format(name))).filter(avg__gt=3)
views.py
from django.views.generic import (TemplateView, ListView)
from festival_list import models
from django.views import generic
from .filter import FestivalFilter
from .filter_mixin import ListFilteredMixin
from el_pagination.views import AjaxListView
from django.contrib.auth.mixins import (LoginRequiredMixin,
PermissionRequiredMixin, UserPassesTestMixin)
from braces.views import SelectRelatedMixin
from django.views.decorators.http import require_GET
from django.urls import (reverse, reverse_lazy)
from django.shortcuts import render
class HomePage(ListFilteredMixin, AjaxListView):
template_name = 'index.html'
page_template = 'index_page.html'
model = models.Festival
paginate_by = 12
context_object_name = 'festivals'
filter_set = FestivalFilter
模板过滤器菜单:
<form action="" method="get" id="filter-form">
<div class="row filters-row text-center">
{% for style in filter.form.style %}
{% if forloop.counter == 4 %}
</div><div class="row filters-row text-center">
{% endif %}
<div class="col col-pad">
{{ style.tag }}
<label for="{{ style.id_for_label }}"><span id="{{ style.id_for_label }}"></span></br> {{ style.choice_label }}</label>
</div>
{% endfor %}
...
...
</div>
任何帮助将不胜感激=]谢谢!