I added a like button to tinyblog. All I did was add a new “likes” model to the “blog” app in Django, added a button to the blog post “view,” and threw together a little JavaScript to increment the database on click and refresh the count on the page. And a few CSS tweaks.
It only took like an entire day. And I don’t know JavaScript at all so it was a lot of back-and-forth with an LLM. I’m still not really comfortable with it. I had the robot talk me through every element of the script, so I understand the logic and what it’s doing, but as with all things LLM, I suspect there’s a more elegant way to do it.
The tricky part came when I also wanted the button to appear on posts on the homepage, which is a different template. Because the blog post template displays a single blog post, I was able to use the single HTML button’s ID to connect to the JavaScript. But that doesn’t work when there are five buttons.
So in the “for” loop that placed the posts on the page, I essentially dynamically generate the HTML button IDs by adding each database object’s primary key to the ID:
<div><button class="like-btn-all" id="like-btn{{ post.id }}" aria-label="Like this post">❤️ {{ post.likes }}</button></div>
Then I had the JavaScript put all the buttons in an iterable object (a “NodeList,” apparently) and could use the post ID associated with each item to increment the “likes” field associated with each post when the button is clicked. Great!
Unfortunately, you can click the button as many times as you want, but I don’t know, seems kind of fun. Click away.
The other feature I added that took another whole day was a “feeds” app, that provides an RSS and an Atom feed. This one wasn’t too difficult, mostly I was struggling with understanding Django’s ALLOWED_HOSTS and how Past Me had laid a few rakes in the grass for Future Me in the settings.py file. Whoops.
Anyway, while I’m not comfortable with relying on an LLM to be able to do this stuff, the fact is that I can do this stuff because I have an LLM to answer questions, help debug, and offer code snippets. It doesn’t do the work for me, not because it can’t write the code but because it can’t understand what I want. It’s always trying to wedge some elaborate boilerplate into something that I would like to be more straight-forward.
I actually think this will get more useful as I grow and my projects get more complicated and the boilerplate has to be more sophisticated. I think a big reason certain tech people think generative AI is going to take over the world is because it is very helpful in the kind of copy-paste work that (let’s face it) makes up a significant part of the act of actually writing code.
Leave a Reply