Django - How to pass multiple selected values from selectpicker from template to views.py

拟墨画扇 提交于 2021-01-07 02:49:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!