HTMX + Django: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Surveying the non-JS-enabled Django app
Lecture: Category model object
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Inside the home view you saw the category model. This is that model, and it's responsible for storing the video category.
0:09
It has two fields, name and image name. The name is what is presented on the screen when a category is shown,
0:17
while the image name is used to store the file name of the banner image. A more robust approach would be to use an image field instead,
0:27
but as I'm not uploading the banner images, I've gone with a somewhat hackier way. Inside the template, I use the static image tag
0:35
to get at the folder and then just append the image name. I've also set up some metadata so the default querying order
0:43
uses the name field in the database. At the bottom of the code, you can see a neat little trick. I've overloaded dunder len.
0:52
This is the method that Python calls if this object is used with the len function. Here, I've had it return the number of videos in this category.
1:01
This gets used in the template. The number beside the category name is how many videos it has. By implementing Dunderlin,
1:10
I'm able to use the length template filter to get at that info. On the homepage, when you click one of these categories,
1:17
you get taken to a page that shows all the videos in that category. Let's look at that next.