我想實現在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:但是當我想要結果僅在點擊搜索按鈕之後被過濾時呢? –