Django的,jQuery和自动完成 [英] Django, JQuery, and autocomplete
问题描述
一些广泛的研究(谷歌搜索)后,我找不到如何设置使用Django和JQuery自动完成当前教程。似乎有多种插件和似乎有没有一致性或标准有关哪些使用或当
After some extensive research (googling), I cannot find a current tutorial on how to set up autocomplete using Django and JQuery. There appears to be a variety of plugins and there appears to be no consistency or standard about which to use or when.
我不是在任的Django或JQuery的亲,但需要一个自动完成的解决方案,是有据可查的,并相当简单的利用。
I'm not a pro at either Django or JQuery, but need an autocomplete solution that is well documented and fairly simple to utilize.
建议?
推荐答案
如果你正在寻找从你的Django模型中,然后像搜索:
If you're looking to search from within your django models then something like:
from django.utils import simplejson
def autocompleteModel(request):
search_qs = ModelName.objects.filter(name__startswith=request.REQUEST['search'])
results = []
for r in search_qs:
results.append(r.name)
resp = request.REQUEST['callback'] + '(' + simplejson.dumps(result) + ');'
return HttpResponse(resp, content_type='application/json')
有关jQuery的自动完成和呼叫:
For the jQuery autocomplete and call:
function searchOpen() {
var search = $('#txtSearch').val()
var data = {
search: search
};
$.ajax({
url: '/search.json',
data: data,
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'searchResult'
});
}
function searchResult(data) {
$( "#txtSearch" ).autocomplete ({
source: data
});
}
最后给它的所有连接输入表单上会碰到这样的:
Finally to connect it all on your input form would have something like:
<input type="text" name="search" id="txtSearch" onkeyup="searchOpen()" />
请注意,这是使用jQuery用户界面,以及除股票jQuery的。
Note, this is using Jquery UI as well in addition to stock jQuery.
这篇关于Django的,jQuery和自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!