From 72e35f67163b67bdf5ccfa709954be21a509b411 Mon Sep 17 00:00:00 2001 From: Bobby Date: Sat, 31 Dec 2022 11:59:38 -0500 Subject: Articles Section --- blog/context_processors.py | 31 +++++++++------- blog/urls.py | 1 + blog/views.py | 21 ++++++++++- templates/blog/articles.html | 49 +++++++++++++++++++++++++ templates/blog/home.html | 8 +--- templates/blog/partials/sidebar.html | 2 +- templates/blog_admin/edit_post.html | 1 - templates/blog_admin/new_post.html | 1 - templates/blog_admin/partials/posts_topbar.html | 16 ++++---- 9 files changed, 97 insertions(+), 33 deletions(-) create mode 100644 templates/blog/articles.html diff --git a/blog/context_processors.py b/blog/context_processors.py index 00d4323d..0faeeb5f 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -1,24 +1,29 @@ from .models import Post, Category, Comment import os from django.conf import settings +from bs4 import BeautifulSoup -def recent_posts(): - recent_posts = Post.objects.filter(is_public=True).order_by('-date')[:5] - for post in recent_posts: - from bs4 import BeautifulSoup - soup = BeautifulSoup(post.body, 'html.parser') +def add_excerpt(post): + 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) + # 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 + if len(excerpt) >= 1000: + break + return excerpt +def add_num_comments(post): + num_comments = Comment.objects.filter(post=post).count() + return num_comments - post.num_comments = Comment.objects.filter(post=post).count() +def recent_posts(): + recent_posts = Post.objects.filter(is_public=True).order_by('-date')[:5] + for post in recent_posts: + post.excerpt = add_excerpt(post) + post.num_comments = add_num_comments(post) return recent_posts def categories(request): diff --git a/blog/urls.py b/blog/urls.py index 526093cc..4c9a000e 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ path('account', views.account, name='account'), path('register', views.register, name='register'), path('search', views.search, name='search'), + path('articles', views.articles, name='articles'), path('articles/', views.post, name='post'), path('articles//comment', views.comment, name='comment'), path('articles//edit_comment', views.edit_comment, name='edit_comment'), diff --git a/blog/views.py b/blog/views.py index 96faa177..50f3b3f6 100644 --- a/blog/views.py +++ b/blog/views.py @@ -6,7 +6,7 @@ import hashlib from random import choice from string import ascii_letters, digits from .models import Category, Post, Comment -from .context_processors import recent_posts, avatar_list +from .context_processors import recent_posts, avatar_list, add_excerpt, add_num_comments from announcements.models import Announcement from users.forms import RegisterForm from users.tokens import CaptchaTokenGenerator @@ -204,4 +204,21 @@ def search(request): # order by date posts = posts.order_by('-date') return render(request, 'blog/search.html', {'title': 'Search', 'posts': posts, 'categories': categories, 'tags': tags, 'cate': category, 'query': query}) - + +from django.core.paginator import Paginator +def articles(request): + page = request.GET.get('page') if request.GET.get('page') else 1 + try: + page = int(page) + except: + page = 1 + + posts = Post.objects.filter(is_public=True).order_by('-date') + posts = Paginator(posts, 10) + posts = posts.page(page) + # add excerpt to each post + for post in posts: + post.excerpt = add_excerpt(post) + post.num_comments = add_num_comments(post) + num_pages = posts.paginator.num_pages + return render(request, 'blog/articles.html', {'title': 'Articles', 'posts': posts, 'num_pages': num_pages, 'page': page}) diff --git a/templates/blog/articles.html b/templates/blog/articles.html new file mode 100644 index 00000000..ab07891c --- /dev/null +++ b/templates/blog/articles.html @@ -0,0 +1,49 @@ +{% extends 'blog/partials/base.html' %} {% block content %} + +

All Posts

+{% if posts %} +
+ {% for post in posts %} +
+

+ {{ post.title }} +

+
+ + Cover image for {{ post.title }} + + {{ post.excerpt | safe }} +
+
+ Continue Reading | {{ post.num_comments }} Comments +
+
+ {% endfor %} +
+{% endif %} +
+ + + {% if page == 1 %} + + + {% else %} + + + {% endif %} + {% load times %} + {% for i in num_pages|times %} + + {% endfor %} + {% if page == num_pages %} + + + {% else %} + + + {% endif %} + +
««{{ i }}»»
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/blog/home.html b/templates/blog/home.html index bf31b189..7c9a8bf7 100644 --- a/templates/blog/home.html +++ b/templates/blog/home.html @@ -4,11 +4,7 @@

Welcome

Welcome to the home of That Computer Scientist. This is my personal - website where I share all of my thoughts, ideas, and experiences. To know - more about me, please visit my About page. If you - would like to connect with me, please visit my - Contact page. Please use the sidebar to navigate - the site. + website where I share all of my thoughts, ideas, and experiences.

This website is a work in progress. I am currently working on adding more @@ -27,7 +23,7 @@ {% if announcements is not None %}

- +
    {% for announcement in announcements %}
  • diff --git a/templates/blog/partials/sidebar.html b/templates/blog/partials/sidebar.html index 0bcdf445..0126d54d 100644 --- a/templates/blog/partials/sidebar.html +++ b/templates/blog/partials/sidebar.html @@ -98,7 +98,7 @@ Blog - + Articles diff --git a/templates/blog_admin/edit_post.html b/templates/blog_admin/edit_post.html index 9cff737d..8c6b24cc 100644 --- a/templates/blog_admin/edit_post.html +++ b/templates/blog_admin/edit_post.html @@ -2,7 +2,6 @@
    - {% include 'blog_admin/partials/posts_topbar.html' %}
    Cover Image
    diff --git a/templates/blog_admin/new_post.html b/templates/blog_admin/new_post.html index 28dcca89..4cad7c77 100644 --- a/templates/blog_admin/new_post.html +++ b/templates/blog_admin/new_post.html @@ -2,7 +2,6 @@
    - {% include 'blog_admin/partials/posts_topbar.html' %}
    {% csrf_token %}
    diff --git a/templates/blog_admin/partials/posts_topbar.html b/templates/blog_admin/partials/posts_topbar.html index 326854a2..506c13dd 100644 --- a/templates/blog_admin/partials/posts_topbar.html +++ b/templates/blog_admin/partials/posts_topbar.html @@ -1,14 +1,12 @@ -
    - Create New Post - {% comment %} Search Users Box {% endcomment %} - - - +
    + + + + Create New Post
    -

    {{ title }}

    -
    -
    +


    +

    {{ title }}

    {% for message in messages %}

    {{ message }}

    {% endfor %} \ No newline at end of file -- cgit v1.2.3