I have a problem with the getlist function in Django. Specifically, I have several checkboxes listed using a for loop. The form is written in HTML without Django Forms. The problem is that the getlist function returns a maximum of two values even if I check more than two checkboxes. Where could the error be?
if 'category' in request.GET: print('Category in request GET.') if len(request.GET.getlist('category')) == 1: print(request.GET.getlist('category')) filters['category_id'] = Category.objects.get( slug='-'.join(request.GET.get('category').lower().split())).id if len(Property.objects.filter(**filters)) == 0: queryset.clear() messages.info(request=request, message='No Results.') return redirect(to='properties') else: request.session['sorted_type'] = 'Newest Properties' request.session['filters'] = filters context.update(sidebar_context(**filters)) queryset.clear() queryset.extend(Property.objects.filter(**filters)) else: print('More than one Category.') filters['category__pk__in'] = [Category.objects.get(slug='-'.join(obj.lower().split())).id for obj in request.GET.getlist('category')] if len(Property.objects.filter(**filters)) == 0: queryset.clear() messages.info(request=request, message='No Results.') return redirect(to='properties') else: print(request.GET.getlist('category')) request.session['sorted_type'] = 'Newest Properties' request.session['filters'] = filters context.update(sidebar_context(**filters)) queryset.clear() queryset.extend(Property.objects.filter(**filters))
<form data-properties-filters-form class="properties__filters__filters-form theme-form" method="get" action="{% url 'properties' %}"><div style="opacity: 0; position: absolute; top: 0; left: 0; height: 0; width: 0; z-index: -1;"><label>leave this field blank to prove your humanity<input type="text" class="url" autocomplete="off" tabindex="-1"></label></div> {% if categories %}<div class="properties__filters__title h4">Category</div><div class="form__row"><div class="form__field"><div data-change-category class="form__input-wrap form__checkbox-wrapper"> {% for category in categories %}<label><input data-checkbox data-input type="checkbox" name="category" value="{{ category|lower }}"{% if category|lower in request.GET.category %}checked{% endif %}><div class="checkbox__label">{{ category }}</div></label> {% endfor %}</div></div></div><hr> {% endif %}
Below is the code on GitHub. The code responsible for filters starts from line 614 of the uncommented properties function in the views.py file. As for the HTML file, it is sidebar.html in the templates folder. Please, can you help me?
I'll add that after selecting each filter, the page reloads, and this is handled by the JavaScript code in the scripts.js file in the static folder. The line of code responsible for this is 2646.