diff options
| -rw-r--r-- | blog/context_processors.py | 14 | ||||
| -rw-r--r-- | 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 @@ <em><a href="#">{{ post.author }}</a> in</em> <em><a href="#">{{ post.category }}</a></em> </p> - <p style="text-align: justify; font-size: 13px; margin-bottom: 0px;"> + <div style="text-align: justify; font-size: 13px; margin-bottom: 0px;"> <span> - <img src="{% url 'ignis:post_image' '200' post.id %}.gif" alt="Cover image for {{ post.title }}" style="float: left; margin-right: 10px; margin-bottom: 10px; width: 200px; height: auto;"> + <img src="{% url 'ignis:post_image' '320' post.id %}.gif" alt="Cover image for {{ post.title }}" style="float: left; margin-right: 11px; margin-top: 4px; width: 320px; height: auto;"> </span> - <span> {{ post.excerpt | safe }} - </span> - </p> + </div> <div class="post-actions" style="clear: both;"> <a href="{% url 'blog:post' post.slug %}">Continue Reading</a> | <a href="{% url 'blog:post' post.slug %}#comments">{{ post.num_comments }} Comments</a> </div> |
