Skip to content

Commit

Permalink
Creating custom template tags and filters to show a list of the lates…
Browse files Browse the repository at this point in the history
…t posts and most commented posts in the sidebar
  • Loading branch information
yahyaaly151989 committed Aug 27, 2023
1 parent c0dcdd1 commit ebc0bd5
Show file tree
Hide file tree
Showing 15 changed files with 361 additions and 3 deletions.
249 changes: 249 additions & 0 deletions Module03/mysite/.idea/inspectionProfiles/Project_Default.xml

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Module03/mysite/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Module03/mysite/.idea/mysite.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Module03/mysite/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Module03/mysite/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion Module03/mysite/blog/templates/blog/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load static %}
{% load blog_tags %}

<!DOCTYPE html>
<html>
Expand All @@ -13,7 +14,25 @@
</div>
<div id="sidebar">
<h2>My blog</h2>
<p>This is my blog.</p>
<p>
This is my blog.
I've written {% total_posts %} posts so far.
</p>

<h3>Latest posts</h3>
{% show_latest_posts 3 %}

<h3>Most commented posts</h3>

{% get_most_commented_posts 2 as most_commented_posts %}
<ul>
{% for post in most_commented_posts %}
<li>
<a href="{{ post.get_absolute_url }}">{{ post.title|upper }}</a>
</li>
{% endfor %}
</ul>

</div>
</body>
</html>
3 changes: 2 additions & 1 deletion Module03/mysite/blog/templates/blog/post/detail.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "blog/base.html" %}
{% load blog_tags %}

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

Expand All @@ -7,7 +8,7 @@ <h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{{ post.body|markdown }}
<p>
<a href="{% url "blog:post_share" post.id %}">
Share this post
Expand Down
7 changes: 7 additions & 0 deletions Module03/mysite/blog/templates/blog/post/latest_posts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul>
{% for post in latest_posts %}
<li>
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
3 changes: 2 additions & 1 deletion Module03/mysite/blog/templates/blog/post/list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "blog/base.html" %}
{% load blog_tags %}

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

Expand Down Expand Up @@ -29,7 +30,7 @@ <h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{{ post.body|markdown|truncatewords_html:30 }}
<hr />
{% endfor %}
{% include "pagination.html" with page=posts %}
Expand Down
Empty file.
Binary file not shown.
Binary file not shown.
26 changes: 26 additions & 0 deletions Module03/mysite/blog/templatetags/blog_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django import template
from ..models import Post
from django.db.models import Count
from django.utils.safestring import mark_safe
import markdown

register = template.Library()

@register.simple_tag
def total_posts():
return Post.objects.count()


@register.inclusion_tag('blog/post/latest_posts.html')
def show_latest_posts(count=5):
latest_posts = Post.objects.order_by('-publish')[:count]
return {'latest_posts': latest_posts}

@register.simple_tag
def get_most_commented_posts(count=5):
return Post.objects.annotate(total_comments=Count('comments')).order_by('-total_comments')[:count]


@register.filter(name='markdown')
def markdown_format(text):
return mark_safe(markdown.markdown(text))
Binary file modified Module03/mysite/db.sqlite3
Binary file not shown.

0 comments on commit ebc0bd5

Please sign in to comment.