使用 Django 数据库/模型的 jQuery 自动完成插件

发布于 2024-10-17 06:50:02 字数 298 浏览 2 评论 0原文

有谁知道如何在 Django 中使用数据库而不是本地值来实现 jQuery 自动完成插件

具体来说,我想实现页面底部提到的“搜索页面替换”功能,数据集将大约有一千个或更多条目,但我无法弄清楚如何让它与数据库的必要字段进行交互。

(我也在寻找一个好的 Python/Django 搜索解决方案用于我的网站 - 这只是一个非常简单的网站。)


感谢您的帮助。

Does anybody know how to implement the jQuery autocomplete plugin in Django, using databases instead of local values?

Specifically I want to implement the "Search Page Replacement" feature mentioned at bottom of the page, the dataset will be approx a thousand or more entries, but I cannot workout how to get it to interact with the necessary fields of my database.

(Am also on the lookout for a good Python/Django search solution to use for my site - which is just a very simple website.)

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-10-24 06:50:02

我最近用一个模型用 jQuery.autocomplete 做了一些事情。

当用户开始写入名称时,seach city 的功能:

根据 jqueryui 文档,要使自动完成工作,您需要这样的输入:

<input id="n" type="text" name="n"/>

因此,我的模板中将 lib 附加到此输入的 javascript 看起来像:

$(document).ready(function(){
     $( "input#n" ).autocomplete({
                            source: "{% url autocomplete_city %}",
                            minLength: 2
        });
});

解析您的 URL必须在你的 urls.py 中写下类似的内容,

urlpatterns = patterns('cities.views',
    url(r'^autocomplete_city/

这意味着我有类似 city.views.autocomplete_city 视图的内容:

def autocomplete_city(request):
    term = request.GET.get('term') #jquery-ui.autocomplete parameter
    cities = City.objects.filter(name__istartswith=term) #lookup for a city
    res = []
    for c in cities:
         #make dict with the metadatas that jquery-ui.autocomple needs (the documentation is your friend)
         dict = {'id':c.id, 'label':c.__unicode__(), 'value':c.__unicode__()}
         res.append(dict)
    return HttpResponse(simplejson.dumps(res))

你还需要什么?开始测试并记住文档是你的朋友请尝试先为自己制作这些东西,谷歌搜索,阅读文档,尝试 3 次,如果不能,stackoverflow 是你的朋友。

, 'autocomplete_city', name='autocomplete_city'), )

这意味着我有类似 city.views.autocomplete_city 视图的内容:

你还需要什么?开始测试并记住文档是你的朋友请尝试先为自己制作这些东西,谷歌搜索,阅读文档,尝试 3 次,如果不能,stackoverflow 是你的朋友。

I rencently did something with jQuery.autocomplete with one model.

funcionality of seach city when user starts to write the name:

according the jqueryui docs to make work the autocomplete you need an input like this:

<input id="n" type="text" name="n"/>

so, the javascript in my template to attach the lib to this input looks like:

$(document).ready(function(){
     $( "input#n" ).autocomplete({
                            source: "{% url autocomplete_city %}",
                            minLength: 2
        });
});

to resolve that URL you have to write something like this in your urls.py

urlpatterns = patterns('cities.views',
    url(r'^autocomplete_city/

that mean that I have something like cities.views.autocomplete_city view:

def autocomplete_city(request):
    term = request.GET.get('term') #jquery-ui.autocomplete parameter
    cities = City.objects.filter(name__istartswith=term) #lookup for a city
    res = []
    for c in cities:
         #make dict with the metadatas that jquery-ui.autocomple needs (the documentation is your friend)
         dict = {'id':c.id, 'label':c.__unicode__(), 'value':c.__unicode__()}
         res.append(dict)
    return HttpResponse(simplejson.dumps(res))

what else de you need? start testing and remember DOCUMENTATIONS ARE YOUR FRIEND please TRY to make the things for yourself first, google it, read the docs, try 3 times, if cant, stackoverflow is your friend.

, 'autocomplete_city', name='autocomplete_city'), )

that mean that I have something like cities.views.autocomplete_city view:

what else de you need? start testing and remember DOCUMENTATIONS ARE YOUR FRIEND please TRY to make the things for yourself first, google it, read the docs, try 3 times, if cant, stackoverflow is your friend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文