How do I remove repition of tags used in posts

I want to display the tags in a dropdown list. But the problem here is that same tags are being displayed per post. So is there any way that I can avoid this repetition? my dropdown list is as below:

  <div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Tags
    </button>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                      {% for post in posts %}
                          {% for tag in post.tags.all %}
                                <a class="dropdown-item" href="{% url 'post_tag' tag.slug %}">
                                    {{ tag.name }}
                                </a>
                          {% endfor %}
                      {% endfor %}

                </div>
            </div>

It would be handy if there are any JS script or anything else to avoid this repetition. Thanks!

Try this:

{% for post in posts %}
    {% for tag in post.tags.all | unique %}
        <a class="dropdown-item" href="{% url 'post_tag' tag.slug %}">                            
        {{ tag.name }}
        </a>
    {% endfor %}
{% endfor %}
Back to Top