我使用django-autocomplete。有两个字段:第一个用于使用自动完成的标记搜索,第二个用于全文搜索。全文搜索有效,但不进行标记搜索。
以下是网页代码:
<div class="content-section-a">
<div class="container">
<div class="row">
<div class="col-lg-12">
<form action="" method="get">
<p>
<span id="id_tag_query-wrapper" class=" autocomplete-light-widget tag_query single" data-widget-bootstrap="normal" data-widget-maximum-values="1" data-widget-ready="1">
<span id="id_tag_query-deck" class="deck" style="display: inline;">
<span data-value="12" class="hilight">
<span style="display: inline-block;" class="remove">
ˣ
</span>
Android
</span>
</span>
<input type="text" name="tag_query-autocomplete" id="id_tag_query-autocomplete" value="" class="form-control autocomplete vTextField" data-autocomplete-choice-selector="[data-value]" data-autocomplete-url="/autocomplete/TagAutocomplete/" placeholder="Tag search" autocomplete="off" style="display: none;">
<select style="display:none" class="value-select" name="tag_query" id="id_tag_query" multiple="multiple">
<option selected="selected" value="12"></option></select>
<span style="display:none" class="remove">ˣ</span>
<span style="display:none" class="choice-template">
<span class="choice prepend-remove append-option-html"></span>
</span>
</span>
</p>
<div class="input-group">
<input type="text" class="form-control" name="search" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-default" value="Search"> Search </button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
查看:
class ThreadsView(ListView):
model = Thread
def get(self, request, *args, **kwargs):
form = TagForm(request.GET)
if request.GET.get('search'):
search_results = watson.search(request.GET.get('search'))
threads = []
for i in search_results:
threads.append(i)
c = RequestContext(request, {'search_list': threads})
if request.GET.get('id_tag_query-deck'):
search_results = watson.search(request.GET.get('id_tag_query-deck'))
threads = []
for i in search_results:
threads.append(i)
c = RequestContext(request, {'search_list': threads})
else:
threads = Thread.objects.all()
c = RequestContext(request, {'threads_list': threads,
'form': form})
return render_to_response('threads.html', context_instance=c)
形式:
class TagForm(forms.Form):
tag_query = autocomplete_light.fields.ChoiceField(
choices=(tuple((tag.name, tag.name) for tag in Tag.objects.all())),
required=False,
label='',
widget=autocomplete_light.widgets.ChoiceWidget(
'TagAutocomplete',
attrs={
'class':'form-control',
'placeholder':'Tag search'
}
)
)
答案 0 :(得分:0)
request.GET.get('id_tag_query-deck'):
为false,您需要使用输入的name
属性,并传递id
的{{1}}属性
也许
span
或request.GET.get('tag_query-autocomplete')
会为您提供所需的值 - 取决于您想要的字段。使用您要查找的request.GET.get('tag_query')
的{{1}}。