jQueryUI自动填充url作为源(使用Django) [英] jQueryUI autocomplete with url as source (am using Django)
问题描述
代码中的jQueryUI / javascript端
我使用Django Web框架进行数据库,页面生成等。我想使用 jQueryUI的自动完成小部件作为我的数据set将包含大约1,000个条目,我想查询数据库。在上面的链接上,它声称您只需提供一个返回JSON数据的网址:
自动完成可以自定义工作
各种数据源,只需
指定源选项。数据
源可以是:*具有本地数据的数组
* a String,指定URL
* a回调
来自网站的href =http://jqueryui.com/demos/autocomplete/#default =nofollow>默认示例,它在我的系统上工作。
但是,如果我更改以下内容:
$(#tags).autocomplete({
source:availableTags,
});
至
$(#tags).autocomplete({
source:/ search /,//提供JSON数据的url
});
自动完成功能根本不起作用。
我已经尝试使url实际返回错误(查看是否使用它)并放入完整的url http:// localhost:8000 / search /
,无效。
Django部分代码
在url.py
...
(r'^ search / $','search'),
...
在views.py
django.http import HttpRequest,HttpResponse
from django.utils import simplejson
...
def search(request):
HttpResponse(simplejson.dumps([hello, world]))
#工作时会实施适当的建议。
我的代码一定有问题,我非常感谢您提供的任何帮助:)
编辑解决方案:
感谢@Thierry意识到之前没有一个 return
所以我现在看起来像这样:
def search(request):
output = [hello,世界]
返回HttpResponse(simplejson.dumps(输出))
!
(这似乎是真正的小bug,浪费了大部分时间,grrr)
def搜索(请求) :
output = [hello,world]
return HttpResponse(output,mimetype =application / javascript)
如果您访问url http:// local主机:8000 / search /
,你应该看到输出。看到输出后,自动完成功能就会起作用。
I am using Django web-framework for database, page generation etc.
jQueryUI / javascript side of the code
I want to use jQueryUI's autocomplete widget, as my data set will contain about 1,000 entries I wanted to query the database. On the link above it claims you can simply provide a url that returns JSON data:
Autocomplete can be customized to work with various data sources, by just specifying the source option. A data source can be:
* an Array with local data * a String, specifying a URL * a Callback
I have taken the default example from the website, which works on my system.
However if I change the following:
$( "#tags" ).autocomplete({
source: availableTags,
});
to
$( "#tags" ).autocomplete({
source: "/search/", // url that provides JSON data
});
the autocomplete functionality doesn't work at all.
I've tried making the url actually return an error (to see if it uses it) and putting in the full url http://localhost:8000/search/
, nothing works.
Django part of the code
In url.py
...
(r'^search/$', 'search'),
...
In views.py
from django.http import HttpRequest, HttpResponse
from django.utils import simplejson
...
def search(request):
HttpResponse(simplejson.dumps(["hello", "world"]))
# Will implement proper suggestions when it works.
There must be something wrong with my code and I would greatly appreciate any help you can offer :)
EDIT SOLUTION:
Thanks to @Thierry realised it didn't have a return
statement before, have added that so I now looks like so:
def search(request):
output = ["hello", "world"]
return HttpResponse(simplejson.dumps(output))
And it actually works!
(It always seems to be the really small bugs that waste the most of of my time, grrr)
I return my ajax response like the following:
def search(request):
output = ["hello", "world"]
return HttpResponse(output, mimetype="application/javascript")
If you access the url http://localhost:8000/search/
, you should see the output. Once you see the output, the autocomplete should work.
这篇关于jQueryUI自动填充url作为源(使用Django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!