Home > Software engineering >  tags are not getting displayed in template, Django
tags are not getting displayed in template, Django

Time:08-19

 context['d_tags'] = Blog.objects.filter(is_published=True).values('tags__name').order_by('tags__name').distinct()
        print(context['d_tags'])

this prints the out put as below

<QuerySet [{'tags__name': 'ev'}, {'tags__name': 'yoga'}]>

how can I show it on templates, tried the following way

 {% for tag in d_tags.all %}
                    <li>{{ tag }}</li>
                    {% endfor %}

gives an out put in template as

 {'tags__name': 'ev'}
{'tags__name': 'yoga'}

but if I do this way

{% for tag in d_tags.all %}
                        <li>{{ tag.name }}</li>
                        {% endfor %}

doesn't gives any thing in display, how I can get just the values in template

CodePudding user response:

You said #root's solution doesnt work, but it should... Are you sure you tried {{tag.tags__name}} and not something else?

Another solution would be opening double for loops...

 {% for tag in d_tags %}
 {% for tag_name in tag %}
                    <li>{{ tag_name }}</li>
 {% endfor %}                    
{% endfor %}

But hey, thats really the same thing as saying {{tag.tags__name}}

  • Related