1👍
✅
A custom template tag can do this for you. You could write an inclusion tag, which will output the rendered template directly:
# yourapp/templatetags/appname_tags.py
def latest_posts(num_posts):
posts = Post.objects.all()[:num_posts]
return {'posts': posts}
register.inclusion_tag('yourapp/partials/latest_posts.html')(latest_posts)
Source:stackexchange.com