Building a Food Delivery App With Django and Python 3: Part 7 Menu View and Search Bar

Video Tutorial

Code on Github

In this tutorial we are going to finish up the last page that we haven’t done yet, which is the menu page. We will list out all of the MenuItem objects and also give the user a search bar to search for specific items. Let’s start with building the basic part of the view for the search and the view for the menu.

In our customer/

We will add some logic for the search in a little bit. Let’s add two urls for these views:

With this, we can build out our menu.html, here is the entire file:

Currently, since we grabbed all of our menu items, this will list out every item in the database. In our html template, there is a search bar. You will see that we are setting a name value of q on the input and we are submitting the form as a get request. This means that whatever we type into the search bar will be added to our url in this format:

we can get whatever q is set as to filter the results. We will add this logic to our MenuSearch view next. You will also see that the url for the form submission set in the action attribute is the menu search view. We need to rerender this template with the filtered results. Here is the updated menu serach view:

We are using Q to add some advanced filters to our MenuItem.objects.filter(). We are matching the object if the search query matches any part of the name, price, or description. We can get the search query with self.request.GET.get(“q”). We are then passing that into the context in our template.

Now when we search for something, it should re render the menu page with a filter for whatever we searched.

With these changes, we should have a working menu page and a working search bar on the menu page. This is where we are going to stop this tutorial. This is all I had planned for this application so this will probably be the last addition to it unless I find something else that would be a good addition for it.