我想集成 Django 和 JavaScript 原型(prototype)库来为表单创建自动完成功能。谁能推荐一个已经在 Django 上尝试过的原型(prototype)功能?有this code和 also this我想知道是否有人会推荐其中一个与 Django 一起使用。谢谢!
最佳答案
我从这个site下载了代码
然后按照那里的指示进行操作,这很简单。只需包含prototype.js、scriptaculous.js 和AutoComplete.js 文件。然后从说明中复制粘贴,并将网址从 assets/ac.php?m=text&s=
更改为 ?m=text&s=
甚至只是 ?s =
如果您只需要一个查询参数。
<input type="text" id="my_ac3" name="my_ac" size="45" autocomplete="off"/>
<script type="text/javascript">
new AutoComplete('my_ac3', '?s=', { delay: 0.25, resultFormat: AutoComplete.Options.RESULT_FORMAT_TEXT });
</script>
在服务器端,在该页面的 View 函数中,通过以下方式启动该函数:
if request.is_ajax():
#match the users input here, perhaps using data from your database and/or regular expressions
text = #response text to return, in my case since I chose Options.RESULT_FORMAT_TEXT as my resultFormat, it's a string where each autocomplete item is separated by '\n'
return HttpResponse(text, mimetype='text/plain') # mimetype is text here in my case
然后将 View 函数的其余部分放在 else 子句下。
关于python - 使用 Prototype 和 Django 的 Ajax 自动完成功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6589207/