Django + Bootstrap3 + typeahead.js远程抓取问题

时间:2013-10-05 06:40:11

标签: django twitter-bootstrap django-views twitter-bootstrap-3 typeahead.js

我是django和bootstrap3的新手,我一直试图通过twitter实现这种类型。

my 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 个答案:

答案 0 :(得分:1)

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

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

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

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

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