搜索表单与django + python [英] search form with django+python
问题描述
我刚刚开始用django + python做一个网站,我想实现一个搜索表单,以便能够搜索我所有的数据库对象。我想要的是当我写一个例子 S
我想要搜索字段显示所有我的对象开始与字母 S
一个列表,就像这个网站下面的标签字段。
I have just started to do a website with django + python and I want to implement a search form to be able to search on all my database objects. What I want is; when I write for an example S
I want the search field to display all my objects that starts with the letter S
in a list, just like the Tags field below on this site.
有没有人有一个很好的想法来实现这个与django?
Does anyone have a good ide to implement this with django?
推荐答案
对于一个体面的django搜索实现,我建议您查看 djapian
。然而,对于你正在做的,我建议使用 ISTARTSWITH
参数进行查询。请考虑以下内容:
For a decent django search implementation, I would recommend looking at djapian
. However, for what you are doing, I would recommend a query using the ISTARTSWITH
parameter. Consider the following:
views.py
def search(req):
if req.GET:
search_term = req.GET['term']
results = ModelToSearch.objects.filter(field__istartswith=search_term)
return render_to_response('search.html', {'results': results})
return render_to_response('search.html', {})
search.html
<html>
<body>
<form>
<input name='S'>
</form>
{% if results %}
Found the following items:
<ol>
{% for result in results %}
<li>{{result}}</li>
{% endfor %}
</ol>
{% endif %}
</body>
</html>
这篇关于搜索表单与django + python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!