I made a few updates to the site, mostly focusing on updating the home page sidebar to have some actual information. The old side bar was just place holder text from the tutorial I found, but the new side bar is populated with the last (3) comments made on the blog.
Before I dig into the coding to make this happen, I want to make a small aside that my old desktop died that was used to make this site and I have moved onto a new laptop which I have been enjoying. I was bit freaked out when I lost the old desktop due to my poor IT backup practices. I deploy this site using git, but should move the site to github. I need to git this done, it looks like Heroku (the site I deploy to) can accommodate both git and github.
The process to get the website back up and running on the new machine was pretty easy. Luckily, my SSD in the old desktop was not fried and I was able to copy all of the website files off of the SSD and xfer onto the new computer. I was able to copy all of my environment variables off of the heroku machine and paste them into the computer. After some struggling to get the local variables set up, I was able to begin working on the site again.
Live Comment Feed
For awhile I wanted to pull a few of the recent comments on the site and have them populate the sidebar like most modern blog sites. Given the popularity of chat gpt, I figured I would give it a go to see if could maybe point me in the right direction.
ChatGPT - Did it Work?
Not really. Here is an example of how the interaction I had with chatgpt:
Me: write python to access django comments model and pull the last 3 comments from the database ChatGPT: Assuming you have set up your Django project and have a comments app with a Comment model, here's an example Python code to access the Comment model and retrieve the last three comments from the database: python from comments.models import Comment last_three_comments = Comment.objects.order_by('-id')[:3] for comment in last_three_comments: print(comment.text) In this code, we import the Comment model from the comments app, and use the objects manager to retrieve the last three comments from the database, sorted by the id field in descending order (so the most recent comments are first). We then loop over the comments and print their text to the console. Note that the id field may not be the best way to sort comments, depending on how your app is set up. You may want to use a created_at or updated_at field instead.
The code that it spat out was correct, but I did not give it enough guidance to properly make this code usable for my specific project. The code produced by chatgpt will extract comments from the database, but this will not in a useable format for rendering into HTML.
Given that I have not played with Django (package to make the website) in awhile, I forgot a lot of the basics. Once you have retrieved the data you want to be in the webpage, you need to pass this data to a view. This was the part that chatgpt missed and that I had forgotten. After some struggling, I was able to pass this data into my views.py file.
class PostListView(ListView): model = Post template_name = 'blog/home.html' # <app>/<model>_<viewtype>.html context_object_name = 'posts' ordering = ['-date_posted'] paginate_by = 5 def get_context_data(self, **kwargs): context = super(PostListView, self).get_context_data(**kwargs) context['last_three_comments']= Comment.objects.order_by('-date_added')[:3] #context['last_three_comments']= Post.objects.prefetch_related('comments').all() return context
Setting up get_context_data enables me to pass both post and comment database information into the home.html file.
With a little tweaking to the html file:
{% for comment in last_three_comments %} <li class="list-group-item list-group-item-light">{{ comment.name }} on {{comment.post.title}} - <a class="article-title" href="{{comment.post.get_absolute_url}}"> {{ comment.body|safe|truncatewords:50}}</a></li> {% endfor %}
the comments will render in and you get a new linked comment you can click on.
A nice update.
Updatable comments
Next item to tackle, allowing users to update their comments.
If you make a comment and you are the user that made the comment you should see an "Update" button next to your comment now.
The code to make this was pretty easy and I am starting to feel more comfortable working django.
Urls, Views and html:
path('commentupdate/<int:comment_pk>/', CommentUpdateView.as_view(), name='comment-update'), class CommentUpdateView(LoginRequiredMixin, UpdateView): model = Comment pk_url_kwarg = "comment_pk" fields = ['body'] success_url = '/' html {% if comment.name == user %} <div> <a class = "btn btn-secondary btn-sm mt-1 mb-1" href = "/commentupdate/{{comment.pk}}">Update</a> </div>
In addition, I added a google tag to the concrete design tool on the site. For the longest time, I thought no one visited this portion of the site, which is probably true, but reviewing the analytics on the site revealed that actually no one had ever visited the site. I realized that I forgot to add a tag to that page. I hope to come back to the concrete design tool soon and document it, I think it is a powerful tool that someone might find useful