0

There are issues with my search code.

Example: If the query does not match the name of an encyclopedia entry, the user should instead be taken to a search results page that displays a list of all encyclopedia entries that have the query as a substring. Almost everything works fine in this application except for two issues:

  1. For example, if the search query was "c," the results should include "cs50," "CSS." But in this case, it shows only one result instead of two, the one at the top of the list, "cs50."

  2. Wen mistyped. If the query does not match the name of an encyclopedia entry, the user should instead be taken to a search results page that displays a list of all encyclopedia entries that have the query as a substring.

In my case, the following error message was displayed: ValueError: The view encyclopedia.views.search didn't return an HttpResponse object. It returned None instead. [27/Nov/2022 15:12:55] "POST /search/ HTTP/1.1" 500 66222

Any idea what is causing those issues? Thanks.

My codes in views.py

    def search(request):
    if request.method == "POST":
        SearchInput = request.POST['q']
        PageContent = MarkdownToHtml(SearchInput)
        if PageContent is not None:
            return render(request, "encyclopedia/entry.html", {
                "title": SearchInput, 
                "ContentMd": PageContent
            })        
        else:
            AppliedFor = util.list_entries()
            References = []
            for entry in AppliedFor:
                if SearchInput.lower() in entry.lower():
                    References.append(entry)
                    return render(request, "encyclopedia/search.html", {
                        "References": References,                       
                    })

1 Answer 1

0

Modified my code by swapping POST and GET method and adding a render template, and it works.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .