【问题标题】:search form with django+python使用 django+python 搜索表单
【发布时间】:2011-05-17 07:57:57
【问题描述】:

我刚开始用 django + python 做一个网站,我想实现一个搜索表单来搜索我所有的数据库对象。我想要的是;当我写示例S 时,我希望搜索字段在列表中显示我所有以字母S 开头的对象,就像本网站下面的标签字段一样。

有没有人有一个用 django 实现这个的好主意?

【问题讨论】:

  • 只是一个简单的搜索:f = searchForm(request.POST) ... pages = Page.objects.filter(name__contains = f.cleaned_data["text"])

标签: python django search


【解决方案1】:

对于一个不错的 django 搜索实现,我建议查看djapian。但是,对于您正在做的事情,我建议使用 ISTARTSWITH 参数进行查询。考虑以下几点:

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>

【讨论】:

    猜你喜欢
    • 2020-07-10
    • 2011-12-08
    • 2018-07-11
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多