Arjun Kashyap
Arjun Kashyap

Reputation: 663

Can you help me find the error for query in django?

I am trying to make a search function which looks through the database to look for similar words in django database.

My views.py:

def result(request):            # Shows the matched result
    query = request.GET.get('q')
    incubators = Incubators.objects.all()
    check = Incubators.objects.filter(incubator_name__icontains=query)
    return render(request, 'main/results.html', {'incubators':incubators,'check': check})

The problem is that my HTML page is not showing any result matching to the database when I know there is. Its just a black HTML page.

{%extends 'main/base.html'%}

{%block content%}
{%for inc in check%}
    <p>{{inc.incubator_name}} <br> {{inc.city_location}}</p>
    <hr>
{%endfor%}
{%endblock%}

I am new to Django and really confused what I am doing wrong here.

This is my HTML file which contains a <form> element for entering the search element:

<form action="/results/" method="GET">
    Search <input type="Name" name="inc_search"><br>
    <input type="submit" name = 'q' value="Submit">
  </form>

And below is my results.html to display the matching elements from the database:

{%extends 'main/base.html'%}
{%block content%}
{%for inc in check%}
    <p>{{inc.incubator_name}} <br> {{inc.city_location}}</p>
    <hr>
{%endfor%}
{%endblock%}

Upvotes: 0

Views: 63

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

Your code doesn't do anything with the value from the request - it just gets it and throws it away. Then it uses the literal value "q" to search against.

You need to assign the value to a variable, and use that variable in the filter.

query = request.GET.get('q')
check = Incubators.objects.filter(incubator_name__icontains=query)

Note, this is a basic programming question, not anything to do with Django in particular.

Upvotes: 1

Related Questions