1👍
I’m not sure you would get much of a performance boost by doing this, the reason being that in one way or another you are going to be reading the result from the hard drive on the second page. Whether this takes the form of pulling from the db again or reading from a cache, the read still needs to take place. A workaround could be to check for the existence of the search term without pulling the full db row in the first view. You can do this by performing a count()
def search(request):
try:
search_string = request.GET['q']
except MultiValueDictKeyError:
raise Http404
if Thing.objects.filter(name=search_string).count() == 1:
return HttpResponseRedirect('/thing/{}'.format(search_string))
else:
# Thing doesn't exist, or there are multiple entries for Thing
This also means that any searches which return multiple results are handled, which your original code isn’t doing.
Source:stackexchange.com