HTMX + Flask: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Feature 2: Active search
Lecture: Concept: Is a request htmx originated?

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw that we need to treat the request differently
0:03 if it's a direct browser initiated request or if it is an htmx
0:10 initiated request. Remember, if the browser is requesting it,
0:13 it wants the entire page. But almost always if htmx is requesting it, it
0:18 wants just part of the page.
0:20 So here's our search implementation that we made a decision.
0:24 We want to show them the video/search.html result
0:29 if it's a full page request,
0:31 either deep linking or just to show the original search page.
0:34 But if it's an htmx request to the same location,
0:38 we want to render the partial search.html one.
0:41 We can use this is_htmx_request property or field that we've added
0:46 to the view model. Where does that come from?
0:50 We realized that htmx sets a header, so we just go to the headers and
0:55 we say do you have any sort of value for something called 'HX-Request'?
1:00 because we don't really care what that value is.
1:02 If that header exists in the collection, htmx added that there for us to say
1:07 hey, this is a request from us.
1:09 So if you need to behave differently for us rather than the regular web browser,
1:13 this is your key to do so.
1:15 And that's how we made search work to render both deep linking as well as the regular response in just a couple lines of code.