我需要一个基本的搜索查询'order by'下拉列表以与RawQuerySet一起使用。我做了一些搜索,找不到具体的例子,虽然我不完全理解的形式和小部件的存在让我觉得我正在重新发明轮子。
def search(request, limit=1000):
query = request.GET.get('q')
mapping = OrderedDict([
('time_desc', 'time_modified DESC'),
('time_asc', 'time_modified ASC'),
('relevance', None)])
sort = request.GET.get('sort')
_ = mapping.get(sort)
order_by = 'ORDER BY %s' % _ if _ else ''
dropdown = {'choices': mapping.keys(),
'selected_choice': sort if sort else 'relevance'}
query = sanitize_query(query)
results = SphinxTable.objects.db_manager('sphinx').raw("SELECT * FROM products WHERE MATCH('%(query)s') %(order_by)s LIMIT %(limit)s" % locals())
return render(request,
'search.html',
{'results': results,
'dropdown': dropdown})
<ul id="filter">
<li>
<h3>{{ dropdown.selected_choice }}▾</h3>
<ul>
{% for choice in dropdown.choices %}
{% if choice == dropdown.selected_choice %}
<li># {{ choice|capfirst }}</li>
{% else %}
<li>
<a href="{{ request.path }}?q={{ request.GET.q }}&sort={{ choice }}">
{{ choice|capfirst }}
</a>
</li>
{% endif %}
{% endfor %}
</ul>
</li>
</ul>