Search code examples
djangosearchfilterviewsearchbar

Searchbar in Django not working: like query is not executed


I am developing an app in Django.

I have contents of a database displayed on my template glossario.html ad I want to implement a search bar to query the database and display only the query results.

So I have set up the toolbar in glossario.html

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> 

<div class="topnav">

    <a id="pulsante_ricerca_avanzata" href="#Ricerca_avanzata">Ricerca avanzata</a>

    <div id="blocco_ricerca_semplice" class="search-container">

      <form method="GET" action="{% url 'glossario' %}">

        <input type="text" placeholder="Ricerca terminologia..." name="q" value="{{request.GET.q}}">  
        <button id="cancel_search_button" type=""><i class="fa fa-trash"></i></button>

        <button id="search_button" type="submit"><i class="fa fa-search"></i></button>

      </form>

    </div>

</div>

In views.py have prepared a function to show only the query results:

def vista_ricerca_semplice(request):

    template = "glossario.html"

    query = request.GET.get('q')

    selected_entries = glossary_entry.objects.filter(Q(Lemma_it__icontains=query))

    context = {'selected_entries':selected_entries}


    return render(request, template, context)

Note:
Lemma_it is the field of my model I want to search and glossary_entry is the name of my model
To tell the truth I am looking for a command to do the query on all the model fields without typing

selected_entries = glossary_entry.objects.filter(Q(Lemma_it__icontains=query) | Q(field2__icontains=query) | Q(field3__icontains=query) ...)    

In app/urls.py I have mapped the url of the search results:

from django.urls import path
from . import views
from .views import vista_ricerca_semplice 

urlpatterns=[
    path('', views.home, name='home'),
    path('glossario', views.glossario, name="glossario"),
    path('glossario', views.vista_ricerca_semplice, name="vista_ricerca_semplice"),
]

But it simply does not work. If for example I type "attempt1", the console returns

[19/Sep/2019 18:08:00] "GET /glossario?q=attempt1 HTTP/1.1" 200 126941

And it updates the page but there is no query. The view does not change. What's the error here?


Solution

  • SOLVED

    As pointed by ,the problem was that I had two views functions pointing at the same path (template).

    This cannot be. One template/path cannot be pointed by more than one view function. So I eliminated all the code related to my previous view function vista_ricerca_semplice

    I solved my problem by changing code in urls.py and views.py like this:

    In views.py:

    def glossario(request):
    
        query = request.GET.get('q')
        template = "glossario.html" 
    
        # query executed
        if query:
    
            query = request.GET.get('q') 
            selected_entries = glossary_entry.objects.filter(Q(Lemma_it__icontains=query))            
            return render(request, template, {'all_entries':selected_entries})
    
        # no query
        else:
    
            all_entries = glossary_entry.objects.all 
            return render(request, template, {'all_entries':all_entries})
    

    In Urls.py

    urlpatterns=[
        path('', views.home, name='home'),
        path('glossario', views.glossario, name="glossario"),
    ]