HTMX + Django: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Feature 2: Active search
Lecture: View with hard coded search results

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I mentioned I was going to use Django queue objects in the search routine, so the first thing I need to do is import those suckers.
0:08 Let me scroll to the bottom of the fileā€¦ and now I'll start to insert the search.
0:21 For now, I'm hardcoding what is being searched for as the string ""indylate"". I'll use this to test the page, and then I'll come back and htmx-ify it.
0:32 The search is going to be pretty simple. It won't support quoted terms, and spaces will be ors.
0:38 This hardcoded text should result in the videos with the words ""indie"" or ""late"".
0:45 Because I want to or the terms together, I'm going to split them up on whitespace and use the pieces to construct a QObject query.
0:52 Here's the code for that. The query here uses the first search term and checks for it in either the title or author
1:10 fields of the video model. I use dunder iContains so that the term can show up anywhere in the field and be case insensitive.
1:21 If there are more terms in the search text, which in this case there are, I then build the same kind of query, ORing it with the existing one.
1:33 This keeps going for as many parts as there are in the search text. Once the loop is complete, I have a large union of QObjects ORed together, and this
1:43 looks for all of the search terms in either the title or author fields. The query then gets executed by calling the filter method.
1:53 The rest of the view is just the typical building of a context and rendering the page.
2:00 So the search text and the resulting hardcoded videos will be rendered using search.html. Let's go add a route. And now I've got a search page.
2:16 Let's go try it out. In the background I reloaded the homepage. The little spyglass thingy here has been added to the navbar. Let's click that.
2:30 And here is the search page with some hardcoded results. All good. Now let's make this puppy actually dynamically interactive.

Talk Python's Mastodon Michael Kennedy's Mastodon