0

我是新來的Django和bootstrap3,我一直在試圖通過twitter實現typeahead。Django + Bootstrap3 + typeahead.js遠程獲取故障

我urls.py

url(r'^name_autocomplete/?q=$', 'home.views.name_autocomplete'), 

我的腳本:

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#navPersonSearch").typeahead({ 
    name: 'people', 
    remote: 'name_autocomplete/?q=%QUERY' 
    }); 
}); 

我看來

def name_autocomplete(request): 
    query = request.GET.get('query','') 
    if(len(query) > 0): 
     print "hello" 
     results = Person.objects.filter(short__istartswith=query) 
     result_list = [] 
     for item in results: 
      result_list.append(item.short) 
    else: 
     result_list = [] 

    response_text = json.dumps(result_list, separators=(',',':')) 
    return HttpResponse(response_text, content_type="application/json") 

的要求似乎是從控制檯輸出打通:

[05/Oct/2013 01:34:12] "GET/HTTP/1.1" 200 2007 
[05/Oct/2013 01:34:12] "GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 304 0 
[05/Oct/2013 01:34:12] "GET /static/bootstrap/css/typeahead.js-bootstrap.css HTTP/1.1" 304 0 
[05/Oct/2013 01:34:12] "GET /static/bootstrap/js/typeahead.js HTTP/1.1" 304 0 
[05/Oct/2013 01:34:15] "GET /name_autocomplete/?q=J HTTP/1.1" 200 2 
[05/Oct/2013 01:34:30] "GET /name_autocomplete/?q=Jac HTTP/1.1" 200 2 

但是鍵入不工作。

回答

1

更換URLPATTERN如下(你不應該指定查詢字符串部分):

url(r'^name_autocomplete/$', 'home.views.name_autocomplete'), 

並在視圖,你query,而JavaScript的發送查詢字符串爲q=;所以替換下面的行:

query = request.GET.get('query','') 

query = request.GET.get('q','') 
+0

是的,作出的預輸入工作(雖然這是一個有點慢),但我無法提交我的形式。 – dl8

+0

@ user2027556,我認爲它值得另一個問題。 – falsetru

+0

嗯,評論劇本讓我把它提交罰款。 – dl8