Book Image

Learning Website Development with Django

Book Image

Learning Website Development with Django

Overview of this book

Table of Contents (18 chapters)
Learning Website Development with Django
Credits
About the Author
About the Reviewers
Preface
Index

Advanced Searching


In Chapter 6, we built a simple search feature into our project. We did so to learn more about Ajax and live form processing. The search page returns bookmarks that contain the query string in the title, and it is implemented using the filter method of the model API. This line of code does the actual searching in our search view:

bookmarks = Bookmark.objects.filter(title__icontains=query)

This was sufficient to get a basic search page working. However, things are not that simple in reality. To see why, let's say that a user entered "Ajax advantages" into the search box. If the database contains a bookmark with the string "Ajax advantages" in its title, it will be returned in the search results. However, if there is a bookmark titled "Advantages of Ajax," it won't be returned, although it's obviously relevant to the search query.

As illustrated by this example, searching for titles that contain the exact query string does not produce satisfactory results. We should split the...