Questions & Answers

Flexible Django Filter (django-filter package)

I'm currently implementing some filters and I'm facing multiple problems. First of all my checkbox works when clicking on the brand title, not the actual: enter image description here

this is my .html code:

<form method="get">
    <div class="toggle-list product-categories">
        <h6 class="title">Sorteer</h6>
        <div class="shop-submenu">
                {{ filter.form.order_by }}
    <div class="toggle-list product-categories">
        <h6 class="title">Merk(en)</h6>
        <div class="shop-submenu">
            {% for f in filter.form.brand %}
                   <input type="checkbox" name="brand" id="brand">
                   <label for="brand">{{ f }}</label>
            {% endfor %}
    <input type="submit" value="Filter">

my filter in

class SortFilter(django_filters.FilterSet):
        ('-discount_sort', 'Hoogste korting'),
        ('-new_price', 'Hoogste prijs'),
        ('new_price', 'Laagste prijs'),
    order_by = django_filters.ChoiceFilter(label='Sorteer op', choices=ORDER_BY_CHOICES, method='filter_by_order',
    brand = django_filters.ModelMultipleChoiceFilter(queryset=Product.objects
                                                     .values_list('brand', flat=True).distinct()
                                                     , widget=forms.CheckboxSelectMultiple, required=False)

    class Meta:
        model = Product
        fields = ['brand']

    def filter_by_order(self, queryset, name, value):
        return queryset.order_by(value)

view function for this certain page:

def eiwit(request):
    # filter alleen eiwitproducten
    eiwit_list = ['eiwitten']
    eiwit_filter = Q()
    for item in eiwit_list:
        eiwit_filter = eiwit_filter | Q(categorie=item)

    products = models.Product.objects.filter(eiwit_filter)
    product_amount = len(products)

    # sorteer filter
    filtered = SortFilter(

    # paginator
    paginator = Paginator(filtered.qs, 12)
    page = request.GET.get('page')
        response =
    except PageNotAnInteger:
        response =
    except EmptyPage:
        response =
    product_front_end = {
        'final_products': response,
        'filter': filtered,
        'count': product_amount,
    return render(request, 'producten/eiwit.html', product_front_end)

Second of all, I would like the filter to be more generic. As in all categories have different amount of brands. As you can see I currently create options by selecting all the 'brand' values within the 'eiwitten' categorie (in Is there a way to call the filter in with a variable (like 'eiwittien'). Which then will be used to adjust the filter in according to the page it is on? This way I have a dynamic filter for all categories.

Answers(1) :

This might help with some of your issues. Your input tag is missing information on the name/id fields. You should be able to populate those by adding template tags to fill the values.

<input type="checkbox" id="BSN" name="BSN"> <label for="BSN">BSN</label>
2023-01-20 00:55:08
Like this?: <input type="checkbox" id={{ f }}, label for={{ f }}>{{ f }}</label>. If I do that I get all the filters 3 times for some reason, I edited it in the question.
2023-01-20 00:55:08
You'll need quotes on your fields. Look up html input tags to dial in the formatting.
2023-01-20 00:55:08
Well I kinda got it working, the problem is clicking on the titles gives me the name in the GET request, whilst clicking on the checkboxes just results in 'on'. <input type="checkbox" name="brand" id="brand"> <label for="brand">{{ f }}</label> . I updated the front-end look of it in the post.