HTMX + Django: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Feature 2: Active search
Lecture: Installing the django-htmx package
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
It's almost time to go back to the view and change our hard-coded search to the actual search.
0:06
Earlier I mentioned that this time around I was going to have a single view do all the things,
0:11
not just the page but the partials as well. This is actually a better design than what I showed
0:17
you in the previous lesson and fits with how you typically do a view with both a get and post when
0:22
dealing with forms. HTMX sets a header any time it calls your URL, and you can check for this
0:29
header to determine whether it is a regular page view or a request for a partial. Rather than write
0:35
that extra header checking code myself, I'm going to use a third-party library called DjangoHTMX.
0:41
It has other neat features as well, but I'll start out with just this one. Let's add it to requirements.txt.
0:48
And since I updated the file, PyCharm prompts me to install the library.
0:55
If you don't have the fancy IDE stuff going on, you can use pip install django-htmx instead.
1:06
To use the library, you need to do a couple of things. First, it needs to be added to installed apps in settings.py. Let me go do that now. now.
1:25
The other thing you need to do is register its middleware.
1:40
This is what actually inserts the data object with htmx information onto the requests object.
1:47
With these two bits in place, the request object now has a member on it called htmx. This gives you access to all of the library's goodness.
1:57
Just go back into the views file and update the search view to deal with the HX get call.