I have a form that relies on htmx to send request to filter data. One text field which is a search as you type and work fine, another is a checkbox. I can see that the QueryDict contains the data, but the filter to be applied when the check box is on is simply not executing
Template section of interest. First input box is working fine
<div class="form__field">
<input id="any_name" class="name-search-input"
name="any_name"
hx-get="/search-plant-name"
hx-trigger="keyup[target.value.length >= 4] changed delay:500ms"
hx-target="#search-results"
hx-push-url="true"
hx-include="#seed_availability"
type="text"
value="{{ any_name }}"
class="form-input"
placeholder="4 letters or more of latin, english, or french name">
</div>
<div>
<!--Input checkbox for seeds available-->
<input
type="checkbox"
id="seed_availability"
name="seed_availability"
value="available-seed"
hx-get="/search-plant-name"
hx-trigger="click"
hx-target="#search-results"
hx-push-url="true"
hx-include="#any_name"
{% if seed_availability %} checked {% endif %}
/>
<label for="seed_availability">Show available seeds only</label>
</div>
views.py
def search_plant_name(request):
if not request.GET:
data = models.PlantProfile.objects.none()
else:
data = models.PlantProfile.objects.all().order_by("latin_name")
search_filter = filters.PlantProfileFilter(request.GET, queryset=data)
object_list = search_filter.qs
print(object_list.count())
print(request.GET)
context = {
"search_filter": search_filter,
"object_list": object_list,
"url_name": "index",
"title": "Plant Profile Filter",
}
template = "project/plant-search-results.html" if request.htmx else "project/plant-catalog.html"
retur
PlantProfileFilterClass:
class PlantProfileFilter(django_filters.FilterSet):
"""Filters used on plant profile search form"""
any_name = django_filters.CharFilter(
method="filter_any_name",
)
seed_availability = django_filters.BooleanFilter(
method="filter_seed_availability",
)
def filter_any_name(self, queryset, name, value):
print("Filter any_name")
return queryset.filter(
Q(**{"latin_name__icontains": value})
| Q(**{"english_name__icontains": value})
| Q(**{"french_name__icontains": value})
)
def filter_seed_availability(self, queryset, name, value):
print("Filter seed_availability"). ### This does not print
if value:
return queryset.filter(**{name: True})
else:
return queryset.exclude(**{name: True})