From a0f9aed243c8825500d4cec17b03fcec398bd1cc Mon Sep 17 00:00:00 2001 From: Bobby Date: Thu, 29 Dec 2022 05:03:17 -0500 Subject: Updates to home page --- blog/context_processors.py | 14 +++++++++++++- templates/blog/home.html | 8 +++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/blog/context_processors.py b/blog/context_processors.py index 5f8ef8c4..00d4323d 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -5,7 +5,19 @@ from django.conf import settings def recent_posts(): recent_posts = Post.objects.filter(is_public=True).order_by('-date')[:5] for post in recent_posts: - post.excerpt = post.body.split('>')[1].split('<')[0] + from bs4 import BeautifulSoup + soup = BeautifulSoup(post.body, 'html.parser') + + # Create excerpt, count min 1000 characters and max upto next paragraph + excerpt = '' + for paragraph in soup.find_all('p'): + excerpt += str(paragraph) + + if len(excerpt) >= 1000: + break + post.excerpt = excerpt + + post.num_comments = Comment.objects.filter(post=post).count() return recent_posts diff --git a/templates/blog/home.html b/templates/blog/home.html index 44c931ae..6abaae39 100644 --- a/templates/blog/home.html +++ b/templates/blog/home.html @@ -62,14 +62,12 @@ {{ post.author }} in {{ post.category }}

-

+

- Cover image for {{ post.title }} + Cover image for {{ post.title }} - {{ post.excerpt | safe }} - -

+
Continue Reading | {{ post.num_comments }} Comments
-- cgit v1.2.3