How do I display the tags used more than once only one time?

The same tags are being repeated and displayed in the dropdown list. It is looping over the tags used in each post but it is not checking if the tags are repated or not. 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>

views.py

def home(request, tag_slug=None):
  posts = Post.objects.all()
  #tag post
  tag = None
   if tag_slug:
     tag = get_object_or_404(Tag, slug=tag_slug)
     posts = posts.filter(tags__in=[tag])
    
return render(request, 'blog/home.html', {'posts':posts})

models has

title = models.CharField(max_length=100)
content = RichTextUploadingField() 
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
image = models.ImageField(default= 'blog_default.jpg', upload_to='blog-pics')
tags = TaggableManager()

I need to solve this issue. I want to use the same tags in different posts but I want to display the tags just once in the dropdown list.

You seem to diplay all tags of all posts, then it makes more sense to just pass a queryset of all tags, so:

def home(request, tag_slug=None):
    posts = Post.objects.all()
    tags = Tag.objects.all()
    if tag_slug:
        tag = get_object_or_404(Tag, slug=tag_slug)
        posts = posts.filter(tags=tag)
    return render(request, 'blog/home.html', {'posts': posts, 'tags': tags})

and work with:

<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    {% for tag in tags %}
        <a class="dropdown-item" href="{% url 'post_tag' tag.slug %}">
            {{ tag.name }}
        </a>
    {% endfor %}
</div>
Back to Top