我一直在使用django书来探索探戈,我使用Bing的搜索API遇到了搜索章节。我试图使用它,但似乎Bing不再提供这些服务。现在我想将搜索功能设置为本地,以便它可以在rango应用中搜索我的类别,但我不知道如何在没有Bing搜索API的情况下执行此操作。如果有的话,任何可以帮助我解决问题的人。提前谢谢。
答案 0 :(得分:0)
以下是在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.