问题
I have a multiselect dropdown (select class="selectpicker" multiple) in my template. I want to transfer the selected values to my search function in views.py so that I can use those parameters to apply queries on my database table and then route the user to Listings page based on search parameters selected.
Please see below my code. Can you please assist as to what I am doing wrong:
Template file:
Colony (All) {% for key,value in colony_choices.items %} {{ value }} {% endfor %}views.py:
if 'colony[]' in request.GET: {% for colony in request.GET.getlist('colony[]') %} if colony: str = str + "," + colony {% endfor %} queryset_list = queryset_list.filter(colony__in=[str])
Once queryset_list is formed, I am passing that as context to my Listings page which is working fine. Can someone please suggest what needs to be changes in template or views.py file.
来源:https://stackoverflow.com/questions/65404630/django-how-to-pass-multiple-selected-values-from-selectpicker-from-template-to