0

I cannot for the life of me get the HTML form to transfer the user's search into my search function within views.py. I've tried this a number of different ways and I either just get no movement (as I have now), or the site simply doesn't load. At the moment I've just put in a HttpResponse as I was trying to troubleshoot where my code is falling down. The HttpResponse isn't being called so it's clearly before here. Please help!

Views.py

def search(request):
    if request.method == "GET":
        return HttpResponse("You have gotten here.")

urls.py

    urlpatterns = [
        path("", views.index, name="index"),
        path("<str:title>", views.title, name="title"),
        path("search", views.search, name="search")

]

layout.html

<body>
        <div class="row">
            <div class="sidebar col-lg-2 col-md-3">
                <h2>Wiki</h2>
                <form a href= "{% url 'search' %}" class="search" method="GET">
                    <input class="search" type="text" name="q" id='q' placeholder="Search Encyclopedia">
                </form>
1
  • is this <form a href= "{% url 'search' %}" class="search" method="GET"> valid HTML? Perhaps check it with Nu Html Checker – DinoCoderSaurus Jul 24 '20 at 16:08
1

There seems to be a clash between the url paths defined as both are taking in similar patterns as arguments:-

path("str:title", views.title, name="title"), path("search", views.search, name="search")

search here is being redirected to views.title with the title="search"

Hope this is helpful.

0

This took me a pretty long time to figure out, and it wasn't explicitly mentioned in the class, but I think you have to use request.GET.get('q') in order to pass the search request argument to your search function in views.py

1
  • Hello, did you ever find an answer to this question? I am having the same issue. – user14567126 Dec 9 '20 at 22:21

You must log in to answer this question.

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