0

我是 django 和 bootstrap3 的新手,我一直在尝试通过 twitter 实现 typeahead。

我的网址.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

但是预输入不起作用。

4

1 回答 1

1

替换 urlpattern 如下(您不应指定查询字符串部分):

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

在视图中,您会得到query,而 javascript 将查询字符串发送为q=; 所以替换以下行:

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

query = request.GET.get('q','')
于 2013-10-05T06:49:05.730 回答