我想实现在Django搜索功能,过滤基于字符串的记录列表,同样存在于django admin list filtering
实现字符串搜索功能
但一个在这里我们不打算进行过滤基于查询字符串的结果from the database
从搜索表单了,相反,我们需要filter from the list
我已经
views.py
def filter_contacts(request):
contacts = ['None <[email protected]>', 'None <[email protected]>',
'None <[email protected]>', 'None <[email protected]>' ...... up to more than 1000]
if request.GET:
if request.GET.has_key('q'):
contacts = **filter the contacts from the above list that contains query string "q" and create a new list**
else:
contacts = contacts
return render_to_response('social_feeds/gmail_contacts.html', {'contacts':contacts})
search_form像
<form class="form-search" action="{% url 'contacts' %}" method="get">
<div class="input-append">
<input type="text" class="span2 search-query" name="q" value="" id="searchbar">
<button type="submit" class="btn btn-primary" value="Search">Search</button>
</div>
</form>
我们当然可以通过looping
从列表过滤query string
和搜索每个字符串,并在列表中,如果它有创建一个新的列表中的每个字符串,
但是因为/假设我们有超过1000个字符串(电子邮件大概如上),那么这个过程应该是very/ultimately
快,所以寻找functionality/process/method
,基于query paramerter
非常快地过滤列表,如假设
如果我们搜索与steve
形式,都包含steve
应该被过滤到一个新的列表,这就是我所寻找的
任何一个可以请让我知道如何实现搜索功能非常快strings/emails
?
您正在寻找的是'autocomplete'。那里有很多文档。还有一个名为'django-autocomplete'的第三方应用程序可能会让你感兴趣 – karthikr
@karthikr:但是当我想要结果仅在点击搜索按钮之后被过滤时呢? –