我的django应用中有一个搜索表单。我想添加一个过滤器选项,所以我添加了一个单选按钮,按名称按字母顺序按升序和降序进行过滤。搜索的值将被传递,但单选按钮值为
<form method='GET' action='{% url "search:catalog-query" %}' class="form my-2 my-lg-0 search-form">
<div class='input-group'>
<input class="form-control" type="text" placeholder="Search" name='q' aria-label="Search" value='{{ request.GET.q }}'>
<span class='input-group-btn'>
<button class="btn btn-outline-success" type="submit">Search</button>
</span>
</div>
<div>
Sort Name:
<label for="id_sort_up">Ascending</label><input type="radio" name="name_sort" id="id_sort" value='{{ request.GET.name_sort }}'>
<label for="id_sort_down">Descending</label><input type="radio" name="name_sort" id="id_sort" value='{{ request.GET.name_sort }}'>
</div>
</form>
urls.py
url(r'^catalog/$', SearchCatalogView.as_view(), name='catalog-query'),
view.py
class SearchCatalogView(ListView):
template_name = "search/view-catalog.html"
print('search catalog')
def get_context_data(self, *args, **kwargs):
context = super(SearchCatalogView, self).get_context_data(*args, **kwargs)
query = self.request.GET.get('q')
context['query'] = query
return context
def get_queryset(self, *args , **kwargs):
print('get_queryset')
request = self.request
method_dict = request.GET
query = method_dict.get('q', None) # method_dict['q']
print(method_dict)
if query is not None:
return AAAA.objects.search(query)
return AAAA.objects.features()
当我打印method_dict时,无论我选择哪个单选按钮,都会得到<QueryDict: {'q': ['searched_option'], 'name_sort': ['']}>
。
答案 0 :(得分:1)
Sort Name:
<label for="id_sort_up">Ascending</label>
<input type="radio" name="name_sort" id="id_sort_up" value='Ascending'>
<label for="id_sort_down">Descending</label>
<input type="radio" name="name_sort" id="id_sort_down" value='Descending'>
会给
method dict <QueryDict: {'q': ['searched_option'], 'name_sort': ['Ascending']}>
要么
method dict <QueryDict: {'q': ['searched_option'], 'name_sort': ['Descending']}>