Listing Tags of a Post in Django CMS and Aldryn NewsBlog

435 Views Asked by At

I am trying to figure out how to display tags belonging to an article created within Aldryn NewsBlog plugin. Unfortunately, I cannot find any documentation on how do it.

I was able to display categories using the following code.

<span style="margin: 0; display: block">
    <h4 style="display:inline-flex">Categories:</h4>
        {% for category in article.categories.all %}
            <a href="/articles/category/{{category.name|lower}}">{{ category.name }} {% if not forloop.last %}, {% endif %}</a>
        {% endfor %}
</span>

For tags, I am using this code:

<span style="margin: 0; padding-bottom: 0; display: block">
    <h4 style="display:inline-flex">Tags:</h4>
        {% for tag in article.tag %}
            <a href="/articles/tag/{{tag.name|lower}}">{{ tag.name }} {% if not forloop.last %}, {% endif %}</a>
        {% endfor %}
</span>

What am I doing wrong? Could anyone tell me how to display tags?

1

There are 1 best solutions below

1
On BEST ANSWER

this is the official tags template of aldryn-newsblog, it worked for me:

{% load i18n apphooks_config_tags %}

<div class="aldryn aldryn-newsblog aldryn-newsblog-tags">
<ul class="list-unstyled">
    <li{% if not newsblog_tag %} class="active"{% endif %}>
        <a href="{% namespace_url "article-list" namespace=instance.app_config.namespace default='' %}">{% trans "All" %}</a>
    </li>
    {% for tag in tags %}
        <li{% if newsblog_tag.id == tag.id %} class="active"{% endif %}>
            <a href="{% namespace_url "article-list-by-tag" tag.slug namespace=instance.app_config.namespace default='' %}">
                {{ tag.name }}
                <span class="badge">{{ tag.article_count }}</span>
            </a>
        </li>
    {% endfor %}
</ul>

https://github.com/aldryn/aldryn-newsblog/blob/master/aldryn_newsblog/boilerplates/bootstrap3/templates/aldryn_newsblog/plugins/tags.html

you're right, that is what you're looking for, with article.tags.all:

{% if article.tags.exists %}
    <ul style="margin-left: 0">
        {% for tag in article.tags.all %}
            <li class="tags"><a href="{% namespace_url 'article-list-by-tag' tag=tag.slug namespace=namespace default='' %}">{{ tag.name }}</a></li>
            {% if not forloop.last %}<span class="separator tags-separator">|</span> {% endif %}
        {% endfor %}
    </ul>
{% endif %}