I am trying to write a code where the user submits a wiki search. If the query is in the given list of entries, the user should be redirected to related entry's page. If the query doesn't match the name of an entry but is a substring of any entry, the user should be taken to a page that lists the related entries. My problem is that I couldn't figure out how to logically implement return statements.
The code below works fine:
def search(request):
sub_list = []
entries = util.list_entries()
search = request.GET.get("q")
for entry in entries:
if search.lower() == entry.lower():
return redirect("entry", title= search)
elif search.lower() in entry.lower():
sub_list.append(entry)
return render(request, "encyclopedia/search.html",{
"title":search,
"sub_list":sub_list
})
But when I try to add another condition for the case where the query doesn't match the name of an entry, either it always returns "Not Found":
for entry in entries:
if search.lower() == entry.lower():
return redirect("entry", title= search)
else:
if search.lower() in entry.lower():
sub_list.append(entry)
return render(request, "encyclopedia/search.html",{
"title":search,
"sub_list":sub_list
})
return HttpResponse("Not Found")
or returns only the first substring match:
for entry in entries:
if search.lower() == entry.lower():
return redirect("entry", title= search)
else:
if search.lower() in entry.lower():
sub_list.append(entry)
return render(request, "encyclopedia/search.html",{
"title":search,
"sub_list":sub_list
})
return HttpResponse("Not Found")
Aucun commentaire:
Enregistrer un commentaire