You need to pass the query
, not the 'query'
string:
def post_search(request):
form = SearchForm()
query = None
results = []
if 'query' in request.GET:
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
results = Post.objects.annotate(
search=SearchVector('title', 'body', 'slug')
).filter(search=query)
# query variable ↑
context = {
'form' : form,
'query' : query,
'results' : results
}
return render(request, 'blog/search.html', context)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…