Priya Sharma
Jan 2026
2 tabs
from django import template
from django.db.models import Count
from blog.models import Post, Tag
register = template.Library()
@register.simple_tag
def total_posts():
"""Return total published posts count."""
return Post.objects.filter(status='published').count()
@register.inclusion_tag('blog/partials/latest_posts.html')
def show_latest_posts(count=5):
"""Render latest published posts."""
posts = Post.objects.filter(
status='published'
).select_related('author').order_by('-published_at')[:count]
return {'posts': posts}
@register.simple_tag
def get_popular_tags(limit=10):
"""Get most used tags."""
return Tag.objects.annotate(
post_count=Count('posts')
).order_by('-post_count')[:limit]
<div class="latest-posts">
<h3>Latest Posts</h3>
<ul>
{% for post in posts %}
<li>
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
<span class="author">by {{ post.author }}</span>
</li>
{% empty %}
<li>No posts yet.</li>
{% endfor %}
</ul>
</div>
2 files · python, django
Explain with highlit
Template tags extend Django's template language. I create simple tags with @register.simple_tag and inclusion tags with @register.inclusion_tag for rendering template snippets with context. Assignment tags store results in template variables. I place tags in templatetags/ directory and load them with {% load my_tags %}. For complex logic, I write a full tag class inheriting from template.Node. Template tags should be stateless and focused on presentation, not business logic. This keeps templates DRY and promotes reusability across the site.
Related snips
python
import os
import stat
for root, _dirs, files in os.walk('/etc'):
for name in files:
path = os.path.join(root, name)
Python security audit script for exposed risky filesystem state
python
auditing
host-security
by Kai Nakamura
1 tab
python
class Product(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
cost = models.DecimalField(max_digits=10, decimal_places=2)
margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
Django model signals vs overriding save
django
python
models
by Priya Sharma
2 tabs
python
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
Django URL namespacing and reverse lookups
django
python
urls
by Priya Sharma
3 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment
class PostType(DjangoObjectType):
Django GraphQL with Graphene
django
python
graphql
by Priya Sharma
2 tabs
python
from django.db.models import Count, Avg, Sum, Q, F
from django.views.generic import TemplateView
from products.models import Product, Order, OrderItem
class DashboardView(TemplateView):
Django aggregation with annotate for statistics
django
python
database
by Priya Sharma
1 tab
python
from rest_framework import permissions
class IsOwner(permissions.BasePermission):
"""Allow only object owner to access."""
Django REST Framework permissions and authorization
django
python
rest
by Priya Sharma
2 tabs
Share this code
Here's the card — post it anywhere.