Adding search

If you look at our home page right now, it should be a list of 50 random products from our database. You can open it at http://127.0.0.1:8000 and it should look similar to this:

Adding search

What we'd like to do is add a basic search form to this page. The form will just be one field that accepts a search term and button to perform the search. The search term will perform a search on the name field of our products list.

Let's create a simple Django form and add it to our page. Create a new main/forms.py file and add the following code:

from django import forms


class SearchForm(forms.Form):
    name = forms.CharField(required=False)

Next, we need to display ...

Get Django Project Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.