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:
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 our search form on the home page. Add the following to the home.html
template, right after the opening tag of the content
block:
<h2>Search</h2> <form action="" method="get"> {{ form.as_p }} <input type="submit" value="Search" /> </form>
Finally, we need to modify our HomeView
so that it uses the user...