keybald
keybald

Reputation: 17

How to print all tags by using taggit?

I have a small problem, namely I would like to be able to print all taggit tags in my Django project, but I can't seem to be able to make it work. Here's what I came up with until now:

views.py

from taggit.models import Tag
tags_all = Tag.objects.all()

and then I'm returning tags_all by using the return function. Then I add the following into my template:

{{ tags_all }}

However, the result I'm getting after rendering the template looks as follows:

<QuerySet [<Tag: security>, <Tag: Internet>]>

I would like to be able to get just tags, without the QuerySet bits. How can I achieve this? Just to remind you, I would like to be able to obtain all tags within the project. I have tried using taggit-templatetags and taggit-templatetags2, but they don't see to work properly with Django 2+.

Your help would be much appreciated.

Thank you in advance.

Upvotes: 0

Views: 103

Answers (2)

Lemayzeur
Lemayzeur

Reputation: 8525

A better look can be achieved that way:

{% for tag in tags_all %}
     {{ tag }} {% if not forloop.last %}, {% endif %}
{% empty %}
     No tags
{% endfor %}

Upvotes: 1

neverwalkaloner
neverwalkaloner

Reputation: 47354

You need to iterate over queryset using for:

{% for tag in tags_all %}
    {{ tag.name }} <br>
{% endfor %}

Also since each tag is object, you can access tag's attribute with ., for example tag.name.

Upvotes: 0

Related Questions