Skip to content

Commit cfb0343

Browse files
authored
Merge pull request #14 from samwelkanda/ft-custom-tags
Ft custom tags
2 parents 22750d9 + c57168b commit cfb0343

File tree

8 files changed

+114
-4
lines changed

8 files changed

+114
-4
lines changed

mysite/apps/blog/templates/blog/post/detail.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends "base.html" %}
2+
{% load blog_tags %}
23

34
{% block title %}{{ post.title }}{% endblock %}
45

@@ -7,7 +8,7 @@ <h1>{{ post.title }}</h1>
78
<p class="date">
89
Published {{ post.publish }} by {{ post.author }}
910
</p>
10-
{{ post.body|linebreaks }}
11+
{{ post.body|markdown }}
1112
<p>
1213
<a href="{% url "blog:post_share" post.id %}">
1314
Share this post
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<ul>
2+
{% for post in latest_posts %}
3+
<li>
4+
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
5+
</li>
6+
{% endfor %}
7+
</ul>

mysite/apps/blog/templates/blog/post/list.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends "base.html" %}
2+
{% load blog_tags %}
23

34
{% block title %}My Blog{% endblock %}
45

@@ -26,7 +27,7 @@ <h2>
2627
<p class="date">
2728
Published {{ post.publish }} by {{ post.author }}
2829
</p>
29-
{{ post.body|truncatewords:30|linebreaks }}
30+
{{ post.body|markdown|truncatewords_html:30 }}
3031
{% endfor %}
3132
{% include "pagination.html" with page=posts %}
3233
{% endblock %}

mysite/apps/blog/templatetags/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from django import template
2+
from django.db.models import Count
3+
from django.utils.safestring import mark_safe
4+
import markdown
5+
from ..models import Post
6+
7+
8+
register = template.Library()
9+
10+
11+
@register.simple_tag
12+
def total_posts():
13+
return Post.published.count()
14+
15+
16+
@register.inclusion_tag('blog/post/latest_posts.html')
17+
def show_latest_posts(count=5):
18+
latest_posts = Post.published.order_by('-publish')[:count]
19+
return {'latest_posts': latest_posts}
20+
21+
22+
@register.simple_tag
23+
def get_most_commented_posts(count=5):
24+
return Post.published.annotate(
25+
total_comments=Count('comments')
26+
).order_by('-total_comments')[:count]
27+
28+
29+
@register.filter(name='markdown')
30+
def markdown_format(text):
31+
return mark_safe(markdown.markdown(text))

mysite/templates/base.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% load static %}
2+
{% load blog_tags %}
23
<!DOCTYPE html>
34
<html>
45
<head>
@@ -12,7 +13,18 @@
1213
</div>
1314
<div id="sidebar">
1415
<h2>My blog</h2>
15-
<p>This is my blog.</p>
16+
<p>This is my blog. I've written {% total_posts %} posts so far.</p>
17+
<h3>Latest posts</h3>
18+
{% show_latest_posts 3 %}
19+
<h3>Most commented posts</h3>
20+
{% get_most_commented_posts as most_commented_posts %}
21+
<ul>
22+
{% for post in most_commented_posts %}
23+
<li>
24+
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
25+
</li>
26+
{% endfor %}
27+
</ul>
1628
</div>
1729
</body>
1830
</html>

poetry.lock

Lines changed: 58 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ python = "^3.7"
99
django = "^3.1.3"
1010
psycopg2-binary = "^2.8.6"
1111
django_taggit = "^1.3.0"
12+
markdown = "^3.3.3"
1213

1314
[tool.poetry.dev-dependencies]
1415

0 commit comments

Comments
 (0)