[英]How to make a search engine in django with suggestions?
I want to make a search bar that works with JavaScript.我想制作一个适用于 JavaScript 的搜索栏。 That way, I can add suggestions like Google does when you are looking for something.
这样,当你在寻找某些东西时,我可以像谷歌那样添加建议。 I already have the search bar and the view to filter the
Patient
s (my model).我已经有搜索栏和视图来过滤
Patient
(我的模型)。
Here is the code这是代码
def search_patient(request):
"""
Will return all the patients that start with the name that the user searches
"""
q = request.POST['q']
results = Patient.objects.filter(names__startswith=q)
for result in results:
result.serialize()
return JsonResponse({'results': results}, status=200)
It is a pretty simple function.这是一个非常简单的 function。 The problem is that I really think that requesting the new string every time the user types a letter or deletes one would blow up the server, and I don't want that.
问题是我真的认为每次用户键入一个字母或删除一个字母时都请求新字符串会炸毁服务器,我不希望这样。 I really need help here, and I am open to every suggestion.
我真的需要帮助,我愿意接受每一个建议。
Thanks!谢谢!
You can put minimum character length on your javascript before the suggestion functionality triggered.在触发建议功能之前,您可以在 javascript 上设置最小字符长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.