We’ll also link the topics to their listing pages, but that’s going to make the topics “for” loop long. Rather than maintain it in two places, we’re going to separate it out to its own template file.
In index.html and post.html, replace “<p class="topics">{{ post.topics.all|join:", " }}</p>” with:
{% include "parts/post_topics.html" %}
Then, make a folder called “parts” in your templates folder, and put this in post_topics.html:
<p class="topics">
{% for topic in post.topics.all %}
<a href="{% url postings.views.topicalPosts topic.slug %}">{{ topic.title }}</a>{% if not forloop.last %},{% endif %}
{% endfor %}
</p>
For loops can be nested; you can also check to see if this is the first or last item using forloop.first or forloop.last. In this case, a comma isn’t necessary after the last item.