My view:
class SearchView(generic.ListView):
paginate_by = 15
template_name = 'search.html'
def get_queryset(self):
query = self.request.GET.get('q', '')
if query:
object_list = Post.objects.filter(
Q(title__icontains=query)
)
else:
object_list = []
return object_list
My template:
<p>You searched for {{ object_list.query }}</p>
Shows SQL…
I want it to show only “TEST” after I use the search bar.
For contrast in a function based view it works:
def search(request):
if request.method == 'GET':
searched = request.GET['q']
recipes = Post.objects.filter(title__contains=searched)
return render(request, 'search.html', {'searched':searched, 'recipes':recipes})
else:
return render(request, 'search.html', {})