我的Django表单会自动添加文本输入字段以及下拉菜单。我想要一个下拉菜单来过滤结果。这是我的代码:
forms.py
from haystack.forms import FacetedSearchForm
from django import forms
class FacetedProductSearchForm(FacetedSearchForm):
desc = forms.ChoiceField(choices=[('n', 'Ascending'), ('y', 'Descending')], label='', initial='y')
def __init__(self, *args, **kwargs):
data = dict(kwargs.get("data", []))
self.retailer = data.get('retailer', [])
self.location = data.get('location', [])
super(FacetedProductSearchForm, self).__init__(*args, **kwargs)
def search(self):
sqs = super(FacetedProductSearchForm, self).search()
if self.cleaned_data['desc'] == 'y':
query = None
sqs = sqs.order_by('-retailer')
else:
sqs = sqs.order_by('retailer')
return sqs
results.html
<div class="tab-filter">
<form id="searchformsort" action="" method="get" >
<select id=sort class="selectpicker" data-style="btn-select"/>
<option>Sort by</option>
<option> {{ form }} </option>
</select>
</form>
</div>
答案 0 :(得分:0)
将此添加到您的ChoiceField:
widget=forms.Select,
来自文档:
小部件是Django对HTML输入元素的表示。
使用Widget,你告诉django使用select
渲染字段希望这个帮助