File tree Expand file tree Collapse file tree 8 files changed +114
-4
lines changed Expand file tree Collapse file tree 8 files changed +114
-4
lines changed Original file line number Diff line number Diff line change 1
1
{% extends "base.html" %}
2
+ {% load blog_tags %}
2
3
3
4
{% block title %}{{ post.title }}{% endblock %}
4
5
@@ -7,7 +8,7 @@ <h1>{{ post.title }}</h1>
7
8
< p class ="date ">
8
9
Published {{ post.publish }} by {{ post.author }}
9
10
</ p >
10
- {{ post.body|linebreaks }}
11
+ {{ post.body|markdown }}
11
12
< p >
12
13
< a href ="{% url "blog:post_share " post.id %}">
13
14
Share this post
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change 1
1
{% extends "base.html" %}
2
+ {% load blog_tags %}
2
3
3
4
{% block title %}My Blog{% endblock %}
4
5
26
27
< p class ="date ">
27
28
Published {{ post.publish }} by {{ post.author }}
28
29
</ p >
29
- {{ post.body|truncatewords :30|linebreaks }}
30
+ {{ post.body|markdown|truncatewords_html :30 }}
30
31
{% endfor %}
31
32
{% include "pagination.html" with page=posts %}
32
33
{% endblock %}
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change 1
1
{% load static %}
2
+ {% load blog_tags %}
2
3
<!DOCTYPE html>
3
4
< html >
4
5
< head >
12
13
</ div >
13
14
< div id ="sidebar ">
14
15
< 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 >
16
28
</ div >
17
29
</ body >
18
30
</ html >
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ python = "^3.7"
9
9
django = " ^3.1.3"
10
10
psycopg2-binary = " ^2.8.6"
11
11
django_taggit = " ^1.3.0"
12
+ markdown = " ^3.3.3"
12
13
13
14
[tool .poetry .dev-dependencies ]
14
15
You can’t perform that action at this time.
0 commit comments