搜索功能 django



我一直在关注 django 的探戈书,我遇到了使用 Bing 的搜索 API 的搜索章节。我试图使用它,但似乎必应不再提供这些服务。现在我想将搜索功能设置为本地,以便它可以在 rango 应用程序中搜索我的类别,但我不知道如何在没有必应搜索 API 的情况下执行此操作。如果有的话,任何可以帮助我解决问题的人。提前谢谢。

下面是一个在 django 中实现基本search的示例:

1( templates/base.html

注意:使用GET方法从form获取搜索输入。

<form name="exampleform" method="GET" action="{% url 'search' %}">

2( views.py

def search(request):
try:
if 'q' in request.GET:# this will be GET now 
querystring = request.GET.get('q')# passing in the query value to search view using the 'q' parameter
if len(querystring) == 0:
return redirect('index')
else:
pass
except:
pass
results = {}
if 'q' in request.GET:
querystring = request.GET.get('q')
if querystring is not None:
results = UserModel.objects.filter(
Q(email__icontains=querystring) |
Q(first_name__icontains=querystring) |
Q(last_name__icontains=querystring)).order_by('pk')# filter returns a list
context = {'results': results}
template = 'templates/search_result.html'
return render(request, template, context)
else:
return redirect('index')
context = {}
else:
return render(request, "templates/search_result.html")

2( urls.py

url(r'^search',views.search, name='search'),

3( templates/search_result.html

{% for each_object in results %} // results is list here so pick each element object using for loop 
<a href="{% url 'user_detail' pk=each_object.pk %}">
<!--Upon successful search object image with hyperlink appears -->
<img src="{{each_object.image.url}}" alt="No Image"></a>
<p>{{each_object.email}}</p>
<p>{{each_object.first_name}}</p>
<p>{{each_object.last_name}}</p>    
{% endfor %}

周围有一些项目需要搜索。这是一个非详尽的列表:djangosearch, django-search (with a dash), django-sphinx.

最新更新