[Answered ]-I can't figure why I'm getting unboundLocalError

2👍

The problem is that you try to return a context dictionary containing cd, results, and total_results even if your if clauses are not satisified. You should fix your indentation:

def post_search(request):
    form = SearchForm()

    if 'query' in request.GET:

        form = SearchForm(request.GET)
        if form.is_valid():
            cd = form.cleaned_data
            results =     SearchQuerySet().models(Post).filter(content=cd['query']).load_all()
        # count total results
            total_results = results.count()
            # Indent this line:
            return render(request, 'blog/post/search.html', {'form': form,
                                                 'cd': cd,
                                                 'results': results,
                                                 'total_results':  total_results})
👤Selcuk

Leave a comment