Abdullah Atif
Abdullah Atif

Reputation: 35

Django SearchVector

Django SearchVector is giving ok results in command line but it is not working on my localhost:8000. Here's how I put it.

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')

     context = {
        'form' : form,
        'query' : query,
        'results' : results }

     return render(request, 'blog/search.html', context)

Upvotes: 1

Views: 311

Answers (2)

Baagrel
Baagrel

Reputation: 76

Have you ever tried single filter quotes? Try it in this way filter(search=query).

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

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)

Upvotes: 1

Related Questions