Implementing a search
Implement def home
in /movie/views.py
with the following (remove the old def home
function and paste the next one):
def home(request): searchTerm = request.GET.get('searchMovie') if searchTerm: movies = Movie.objects.filter(title__icontains=searchTerm) else: movies = Movie.objects.all() return render(request, 'home.html', {'searchTerm':searchTerm, 'movies': movies})
Let's see what's happening in the code. We retrieve the search term entered (if any) from the searchMovie
input:
searchTerm = request.GET.get('searchMovie')
If a search term is entered, we call the model's filter
method...