aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-01-23 05:04:06 -0500
committerBobby <[email protected]>2023-01-23 05:04:06 -0500
commit81a6efc0cc50d9e75098bb7a2d951f3079bdc609 (patch)
tree2b5ffbe4fe1017265f8bc549c5666deb8d777855
parent85132d4bf7658c47afba122bab9f050c7172b122 (diff)
downloadthatcomputerscientist-81a6efc0cc50d9e75098bb7a2d951f3079bdc609.tar.xz
thatcomputerscientist-81a6efc0cc50d9e75098bb7a2d951f3079bdc609.zip
categories section
-rw-r--r--blog/urls.py2
-rw-r--r--blog/views.py21
-rw-r--r--templates/blog/articles.html4
-rw-r--r--templates/blog/categories.html26
-rw-r--r--templates/blog/partials/sidebar.html24
5 files changed, 71 insertions, 6 deletions
diff --git a/blog/urls.py b/blog/urls.py
index 908bca34..db884a8e 100644
--- a/blog/urls.py
+++ b/blog/urls.py
@@ -15,5 +15,7 @@ urlpatterns = [
path('articles/<str:slug>/delete_comment/<int:comment_id>', views.delete_comment, name='delete_comment'),
path('archives', views.archives, name='archives'),
path('archives/<str:date>', views.articles, name='archive_posts'),
+ path('categories', views.categories, name='categories'),
+ path('categories/<str:cg>', views.articles, name='category_posts'),
path('~<str:username>', views.user_activity, name='user_activity'),
]
diff --git a/blog/views.py b/blog/views.py
index b1868b7a..ae55b572 100644
--- a/blog/views.py
+++ b/blog/views.py
@@ -215,7 +215,7 @@ def search(request):
posts = posts.order_by('-date')
return render(request, 'blog/search.html', {'title': 'Search', 'posts': posts, 'categories': categories, 'tags': tags, 'cate': category, 'query': query})
-def articles(request, date=None):
+def articles(request, date=None, cg=None):
type = 'articles'
page = request.GET.get('page') if request.GET.get('page') else 1
order_by = request.GET.get('order_by') if request.GET.get('order_by') else 'date'
@@ -227,6 +227,8 @@ def articles(request, date=None):
except:
page = 1
+ posts = Post.objects.filter(is_public=True)
+
if date:
date_month = date.split('_')[0] # month name like 'Decemeber'
date_year = date.split('_')[1] # year like '2019'
@@ -234,8 +236,15 @@ def articles(request, date=None):
posts = Post.objects.filter(is_public=True, date__month=date_m, date__year=date_year)
type = 'articles-archive'
date = date_month + ' ' + date_year
- else:
- posts = Post.objects.filter(is_public=True)
+
+ if cg:
+ cg = str.lower(cg)
+ if category and cg != category and category != 'all':
+ return redirect(reverse('blog:categories') + '/{}'.format(category))
+ category = cg
+ posts = Post.objects.filter(is_public=True, category__slug=cg)
+ type = 'articles-category'
+
posts = posts.order_by('-' + order_by) if direction == 'desc' else Post.objects.filter(is_public=True).order_by(order_by)
if category and category != 'all':
@@ -249,7 +258,7 @@ def articles(request, date=None):
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, 'order_by': order_by, 'direction': direction, 'categories': categories, 'category': category, 'type': type, 'date': date if date else ''})
+ return render(request, 'blog/articles.html', {'title': 'Articles', 'posts': posts, 'num_pages': num_pages, 'page': page, 'order_by': order_by, 'direction': direction, 'categories': categories, 'category': category, 'type': type, 'date': date if date else '', 'cg': cg if cg else ''})
def user_activity(request, username):
user = User.objects.get(username=username)
@@ -272,3 +281,7 @@ def user_activity(request, username):
def archives(request):
archives = Post.objects.filter(is_public=True).dates('date', 'month', order='DESC')
return render(request, 'blog/archives.html', {'title': 'Archives', 'archives': archives})
+
+def categories(request):
+ categories = Category.objects.all()
+ return render(request, 'blog/categories.html', {'title': 'Categories', 'categories': categories})
diff --git a/templates/blog/articles.html b/templates/blog/articles.html
index 67e95e81..d228925a 100644
--- a/templates/blog/articles.html
+++ b/templates/blog/articles.html
@@ -4,6 +4,8 @@
<h2 style="margin-top:0px;"> All Posts</h2>
{% elif type == 'articles-archive' %}
<h2 style="margin-top:0px;"> Posts made in {{ date }}</h2>
+{% elif type == 'articles-category' %}
+<h2 style="margin-top:0px;"> Posts made in Category: {{ category }}</h2>
{% endif %}
<form id="filters" method="get">
@@ -18,6 +20,7 @@
<option value="asc" {% if direction == 'asc' %}selected{% endif %}>Ascending</option>
<option value="desc" {% if direction == 'desc' %}selected{% endif %}>Descending</option>
</select>
+ {% if type != 'articles-category' %}
<label for="category">&nbsp;&nbsp;In Category: </label>
<select name="category">
<option value="all" {% if category == 'all' %}selected{% endif %}>All</option>
@@ -25,6 +28,7 @@
<option value="{{ cat.slug }}" {% if category == cat.slug %}selected{% endif %}>{{ cat.name }}</option>
{% endfor %}
</select>
+ {% endif %}
<span>&nbsp;&nbsp;</span>
<input type="submit" value="Apply" class="button button-special" />
</form>
diff --git a/templates/blog/categories.html b/templates/blog/categories.html
new file mode 100644
index 00000000..6708f3d8
--- /dev/null
+++ b/templates/blog/categories.html
@@ -0,0 +1,26 @@
+{% extends 'blog/partials/base.html' %} {% block content %}
+<h2 style="margin-top:0px;">Categories</h2>
+{% load static %}
+{% if categories %}
+<div id="categories-area">
+ <ul>
+ {% for category in categories %}
+ <li>
+ <span>
+ <img src="{% static 'images/site/icons/books.gif' %}" alt="Archive" border="0">
+ </span>
+ <span>
+ <a href="{% url 'blog:categories' %}/{{ category.slug }}">
+ {{ category }}
+ </a>
+ </span>
+ </li>
+ {% endfor %}
+ </ul>
+</div>
+{% else %}
+<p>No categories found.</p>
+{% endif %}
+
+{% endblock %}
+
diff --git a/templates/blog/partials/sidebar.html b/templates/blog/partials/sidebar.html
index bb15e0e4..a03424e2 100644
--- a/templates/blog/partials/sidebar.html
+++ b/templates/blog/partials/sidebar.html
@@ -128,7 +128,7 @@
<img src="{% static 'images/site/icons/books.gif' %}" alt="Categories" border="0">
</span>
<span>
- <a href="#">
+ <a href="{% url 'blog:categories' %}">
Categories
</a>
</span>
@@ -177,6 +177,16 @@
</span>
</li>
{% endfor %}
+ <li>
+ <span>
+ <img src="{% static 'images/site/icons/cabinet.gif' %}" alt="Archives" border="0">
+ </span>
+ <span>
+ <a href="{% url 'blog:archives' %}">
+ All Archives...
+ </a>
+ </span>
+ </li>
</ul>
</div>
@@ -189,12 +199,22 @@
<img src="{% static 'images/site/icons/books.gif' %}" alt="Archive" border="0">
</span>
<span>
- <a href="#">
+ <a href="{% url 'blog:categories' %}/{{ category.slug }}">
{{ category }}
</a>
</span>
</li>
{% endfor %}
+ <li>
+ <span>
+ <img src="{% static 'images/site/icons/books.gif' %}" alt="Categories" border="0">
+ </span>
+ <span>
+ <a href="{% url 'blog:categories' %}">
+ All Categories...
+ </a>
+ </span>
+ </li>
</ul>
</div>