Template Tags

DJ Press provides a rich set of template tags to help you build your blog templates. These tags allow you to retrieve and display data from your blog, create navigation menus, format dates, and more.

Loading Template Tags

To use any of the following tags, you must load the djpress_tags in your template file:

{% load djpress_tags %}

Tag Categories

The template tags in DJ Press are organised into several functional categories:

  1. Data Access Tags - Tags that start with get_ retrieve data from the database without any HTML formatting

  2. Display Tags - Format and display data with appropriate HTML (e.g., site_title_link, blog_categories)

  3. Post Content Tags - Handle rendering post content, titles, dates, etc. (e.g., post_title, post_content)

  4. Navigation Tags - Generate navigation elements like menus (e.g., site_pages_list, blog_categories)

  5. Utility Tags - Additional helper tags for common operations

Table of Contents

Data Access Tags

These tags retrieve data from your blog without adding any HTML formatting. They’re useful when you want to access data but apply your own custom formatting.

get_posts

Returns all published posts as a queryset. Use this tag to access all published blog posts in your templates.

Returns: queryset of all published posts.

Related Topics: See url_structure.md for URL patterns and themes.md for how to use posts in your theme.

get_posts Example

This is useful for building an index page with all posts:

{% get_posts as all_posts %}

{% for post in all_posts %}
<ul>
  <li>{{ post.title }}</li>
</ul>
{% endfor %}

get_recent_posts

Returns the most recent published posts. Tries to be efficient by checking if there’s a posts object in the context that can be used.

Returns: queryset of recent published posts.

get_recent_posts Example

{% get_recent_posts as recent_posts %}

<h3>Recent Posts</h3>
<ul>
  {% for post in recent_posts %}
  <li><a href="{{ post.url }}">{{ post.title }}</a></li>
  {% endfor %}
</ul>

get_pages

Get all published pages as an iterable queryset. This can be useful if you want to build your own menu or list of pages on the blog.

Returns: queryset of published pages that are sorted by the menu_order field, and then by the title field.

get_pages Example

Get the pages and display as a list:

{% get_pages as pages %}
<ul>
  {% for page in pages %}
    <li>
      <a href="{% url 'djpress:single_post' page.slug %}">{{ page.title }}</a>
    </li>
  {% endfor %}
</ul>

Outputs the following:

<ul>
  <li>
    <a href="/about/">About me</a>
  </li>
  <li>
    <a href="/contact/">Contact me</a>
  </li>
</ul>

Also see site_pages for a tag that produces similar output to the above but with just a single template tag.

get_categories

Get all categories as an iterable queryset. This can be useful if you want to build your own menu or list of categories on the blog.

Also see blog_categories for a tag that produces similar output to the below example, but with just a single template tag.

Returns: queryset of categories that are sorted by the menu_order field, and then by the title field.

get_categories Example

Get the categories and display as a list:

{% get_categories as categories %}
<ul>
  {% for category in categories %}
    <li>
      <a href="{% url 'djpress:category_posts' category.slug %}">{{ category.title }}</a>
    </li>
  {% endfor %}
</ul>

Outputs the following:

<ul>
  <li>
    <a href="/category/general/">General</a>
  </li>
  <li>
    <a href="/category/news/">News</a>
  </li>
</ul>

get_tags

Returns all tags as a queryset.

Returns: queryset of all tags.

get_tags Example

{% get_tags as all_tags %}
<div class="tag-cloud">
  {% for tag in all_tags %}
    <a href="{% url 'djpress:tag_posts' tag.slug %}" class="tag">{{ tag.title }}</a>
  {% endfor %}
</div>

get_post_title

Returns the title of a post from the current context.

get_post_title Parameters

  • include_empty (boolean): Whether to include the title if it is empty, using the post_title property fallback. Default is false.

Returns: string - the title of the post.

get_post_title Example

{% get_post_title as title %}
<meta property="og:title" content="{{ title }}">

get_post_url

Returns the URL of a post from the current context.

Returns: string - the URL of the post.

get_post_url Example

{% get_post_url as post_url %}
<meta property="og:url" content="{{ post_url }}">

get_post_author

Returns the author display name from the current context.

Returns: string - the author display name.

get_post_author Example

{% get_post_author as author %}
<meta property="article:author" content="{{ author }}">

get_post_date

Returns the date of a post from the current context.

Returns: string - the date of the post in the format “Mon DD, YYYY”.

get_post_date Example

{% get_post_date as post_date %}
<meta property="article:published_time" content="{{ post_date }}">

Display Tags

These tags retrieve data and format it with HTML, ready to be displayed in your templates.

site_title

Returns the site title as configured in the settings with the SITE_TITLE variable. If no site title has been configured, this will return the default title: “My DJ Press Blog”.

Returns: the SITE_TITLE value as a string.

Related Topics: See configuration.md for how to configure the site title and other settings.

site_title Example

This is useful for the HTML title tag:

<title>{% site_title %}</title>

page_title

Returns the title of the page, depending on the context of the page. A different title is returned for a category page, a blog page, author page, etc. This is useful to be used in the <title> tag of the template.

Returns: A string with no HTML formatting.

page_title Parameters

  • pre_text (optional): text to be displayed before the page title

  • post_text (optional): text to be displayed after the page title

page_title Examples

The tag with no options for a single blog page or post:

<title>{% page_title %}</title>

Outputs the following:

<title>My blog post title</title>

The tag can be combined with the site_title tag as well as using the post_text field.

<title>{% page_title post_text=" | " %}{% site_title %}</title>

Outputs the following:

<title>My blog post title | My DJ Press Blog</title>

Or you can get creative with an emoji:

<title>{% page_title pre_text="🚀 " post_text=" | " %}{% site_title %}</title>

Outputs the following:

<title>🚀 My blog post title | My DJ Press Blog</title>

Category pages will show the title of the category, and author pages will show the display name of the author.

Post Content Tags

These tags help you display the content of posts.

post_title

Display the title of a post, with intelligent handling of linking depending on the context.

  • In a single post view, displays just the title with no link.

  • In a posts list view, displays the title with a link to the full post.

Returns: HTML with the post title, marked as safe.

Related Topics: See themes.md for more about post display in templates and url_structure.md for post URL patterns.

post_title Parameters

  • outer_tag (optional): The outer HTML tag for the title. Accepted options are “h1”, “h2”, “h3”, “h4”, “h5”, “h6”, “p”, “div”, “span”. If not provided, no outer tag is used.

  • link_class (optional): The CSS class(es) to apply to the link if the title is linked.

  • force_link (optional): Boolean. If true, always links the title even in a single post view. Default is false.

  • include_empty (optional): Boolean. If true, includes the title even if it is empty, using the post_title property fallback. Default is false.

post_title Exampless

Basic usage in a template:

{% post_title %}

In a single post view, this will output:

My Blog Post Title

In a posts list view, this will output:

<a href="/2025/05/blog-post/" title="My Blog Post Title">My Blog Post Title</a>

With an outer tag and custom link class:

{% post_title outer_tag="h1" link_class="post-title-link" %}

In a single post view with microformats enabled, this will output:

<h1 class="p-name">My Blog Post Title</h1>

In a posts list view with microformats enabled, this will output:

<h1 class="p-name">
  <a href="/2025/05/blog-post/" title="My Blog Post Title" class="u-url post-title-link">My Blog Post Title</a>
</h1>

Force link in a single post view:

{% post_title force_link=True %}

This will always generate a link to the post, even in single post view.

post_content

Display the content of a post with intelligent handling of both single posts and post lists.

  • In a single post view, returns the full content of the post.

  • In a posts list view, returns the truncated content with a “read more” link.

Returns: HTML content for the post which has been marked as safe.

Related Topics: See themes.md for more about content display and configuration.md for content truncation settings.

post_content Parameters

  • outer_tag (optional): The outer HTML tag to wrap the content in. Accepted options are “section”, “div”, “article”, “p”, “span”. If not provided, no outer tag is used.

  • read_more_link_class (optional): The CSS class(es) to apply to the “read more” link.

  • read_more_text (optional): The text to use for the “read more” link. If not provided, defaults to “Read more”.

post_content Examples

Basic usage in a template:

{% post_content %}

This will output the full content of a post in a single post view, or the truncated content with a “read more” link in a posts list view.

With custom read more text and an outer tag:

{% post_content outer_tag="div" read_more_link_class="btn btn-primary" read_more_text="Continue reading..." %}

This will output:

<!-- In a single post view -->
<div class="e-content">
  <p>This is the full content of the post...</p>
</div>

<!-- In a posts list view -->
<div class="e-content">
  <p>This is the truncated content of the post...</p>
  <a href="/2023/05/sample-post/" class="btn btn-primary">Continue reading...</a>
</div>

Note: The class="e-content" will only be added if Microformats are enabled in the configuration.

post_date

Display the date of a post, optionally with links to the archives if archive functionality is enabled.

Returns: HTML with formatted date information, marked as safe.

Related Topics: See url_structure.md for archive URL patterns and configuration.md for date/archive settings.

post_date Parameters

  • link_class (optional): The CSS class(es) to apply to the date links.

post_date Examples

Basic usage in a template:

{% post_date %}

If archive functionality is disabled, this will output:

May 20, 2025

If archive functionality is enabled, this will output links to the date archives:

<a href="/2025/05/" title="View all posts in May 2025">May</a> <a href="/2025/05/20/" title="View all posts on 20 May 2025">20</a>, <a href="/2025/" title="View all posts in 2025">2025</a>, 10:30 AM.

With microformats enabled, the output will include time tag:

<time class="dt-published" datetime="2025-05-20T10:30:00+00:00">
  <a href="/2025/05/" title="View all posts in May 2025">May</a> <a href="/2025/05/20/" title="View all posts on 20 May 2025">20</a>, <a href="/2025/" title="View all posts in 2025">2025</a>, 10:30 AM.
</time>

With a custom link class:

{% post_date link_class="date-link" %}

This will add the “date-link” class to all the date links.

post_author

Return the author link for a post.

Returns: HTML with the author name and link, marked as safe.

post_author Parameters

  • link_class (optional): The CSS class(es) to apply to the link.

post_author Examples

{% post_author %}

If author links are enabled, this will output:

<a href="/author/johndoe/" title="View all posts by John Doe"><span class="p-author">John Doe</span></a>

If author links are disabled, this will output just the author name:

<span class="p-author">John Doe</span>

The microformat class p-author will only be added if Microformats are enabled.

post_category_link

Return the category link for a post.

Returns: HTML with the category link, marked as safe.

post_category_link Parameters

  • category: The category object of the post.

  • link_class (optional): The CSS class(es) to apply to the link.

post_category_link Examples

{% for category in post.categories.all %}
  {% post_category_link category %}
{% endfor %}

If category functionality is disabled, this will output just the category name:

General

If category functionality is enabled, this will output a link to the category:

<a href="/category/general/" title="View all posts in the General category">General</a>

Utility Tags

These tags provide additional helper functions for your templates.

have_posts

Return the posts in the context, providing a convenient way to check if there are posts to display.

Returns: list of Post objects or a Page object of posts.

have_posts Example

{% have_posts as posts_list %}
{% if posts_list %}
  <h2>We have posts!</h2>
  {% for post in posts_list %}
    <h3>{{ post.title }}</h3>
  {% endfor %}
{% else %}
  <p>No posts to display.</p>
{% endif %}

category_title

Return the title of a category from the current context.

Returns: string or HTML-formatted string with the category title.

category_title Parameters

  • outer (optional): The outer HTML tag for the category. Allowed values: “h1”, “h2”, “h3”, “h4”, “h5”, “h6”, “p”, “div”, “span”.

  • outer_class (optional): The CSS class(es) for the outer tag.

  • pre_text (optional): The text to prepend to the category title.

  • post_text (optional): The text to append to the category title.

category_title Examples

{% category_title outer="h1" outer_class="category-title" pre_text="Category: " %}

This will output:

<h1 class="category-title">Category: General</h1>

tag_title

Return the title of a tag from the current context.

Returns: string or HTML-formatted string with the tag title.

tag_title Parameters

  • outer (optional): The outer HTML tag for the tag title. Allowed values: “h1”, “h2”, “h3”, “h4”, “h5”, “h6”, “p”, “div”, “span”.

  • outer_class (optional): The CSS class(es) for the outer tag.

  • pre_text (optional): The text to prepend to the tag title.

  • post_text (optional): The text to append to the tag title.

tag_title Examples

{% tag_title outer="h1" outer_class="tag-title" pre_text="Posts tagged with: " %}

This will output:

<h1 class="tag-title">Posts tagged with: Python</h1>

If multiple tags are in the context, they will be joined with commas:

<h1 class="tag-title">Posts tagged with: Python, Django</h1>