33 : Search Functionality using FastAPI and Sqlalchemy


If you notice, we have a search form in the top right of navbar. But, currently it is as good as Ziyan's song. So, in this tutorial we are going to implement this feature.
Listview in FastAPI with a searchform in navbar

Currently, there is a problem with our form, Whenever we submit a form by clicking on the button, a request is sent to our fastapi server which should have form-data as key and value pairs. But our form input field does not have a "name" attribute,therefor the request will not have a key. So, we need to add name="somefiledname" in our form's input field. Edit the file templates > components > navbar.html : and add an attribute name="query"

<form class="d-flex" action="/search/">
  <input class="form-control me-2" name="query" type="search" placeholder="Search" aria-label="Search">
  <button class="btn btn-outline-success" type="submit">Search</button>

Now, try to submit the form and see the changes in the url-bar. Now, we need to implement the logic to search. Had we been using raw SQL queries we might have use something like: select * from job where title like some-pattern since we are using ORM, we need to write in the language ORM will understand and then ORM will itself translate it to a proper SQL query. Add this new function in db > repository > jobs.py

def search_job(query: str, db: Session):
    jobs = db.query(Job).filter(Job.title.contains(query))
    return jobs

I believe the code is self-explanatory, users will type something in the search box, which is received by the backend and the backend passes the query and the db session to this function. Finally, this function searches for a job with title containing the query. There is one missing piece though. How does this function named search_job receives the query? There should be some endpoint to receive user's query and pass to this function. Lets implement that endpoint by adding below snippet at webapps > jobs > route_jobs.py

from typing import Optional
from db.repository.jobs import search_job


def search(
    request: Request, db: Session = Depends(get_db), query: Optional[str] = None
    jobs = search_job(query, db=db)
    return templates.TemplateResponse(
        "general_pages/homepage.html", {"request": request, "jobs": jobs}

All, done. I reused the already existing homepage.html file. It has the responsibility of showing job objects in the form of cards. So, if we pass the searched jobs to this HTML file. We should still see the searched jobs in a card-like format. Time to test out our code. Enter something in the searchbox and hit search button.

Final git commit : https://github.com/nofoobar/JobBoard-Fastapi/commit/fe8c9fcb657c7ea36744fb6f7b7f52389e8174a3

Prev: 32 : Deleting … Next: 34 : Autocomplete …

Brige the gap between Tutorial hell and Industry. We want to bring in the culture of Clean Code, Test Driven Development.

We know, we might make it hard for you but definitely worth the efforts.



Refund Policy

Follow us on our social media channels to stay updated.

© Copyright 2022-23 Team FastAPITutorial