Home.html isn't displaying tags

The tags aren't showing in the home page. but it appears in post_detail.html. Its not working as I want it to work. Can anyone fill me up with mistakes here and solutions?

blog/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])


context={
'posts': posts,     #introduces the content added in Post Class
'tag':tag,
}

return render(request, 'blog/home.html', context)

class PostListView(ListView):
    model=Post
    template_name = 'blog/home.html'    # <app>/<model>_<viewtype>.html
    context_object_name= 'posts'
    ordering = ['-date_posted']

class PostDetailView(DetailView):
     model=Post

blog/urls.py:

urlpatterns = [
path('', PostListView.as_view(), name='blog-home'), 
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('tag/<slug:tag_slug>/',views.home, name='post_tag'),

templates/blog/base.html

<div class="col-md-4">
     <div class="content-section">
         <h3>Sidebar</h3>
            <p class='text-muted'>Informations
      <ul class="list-group">
          <li class="list-group-item list-group-item-light" style="text-align:center">
             <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 tag in post.tags.all %}
                          <a class="dropdown-item" href="{% url 'post_tag' tag.slug %}">{{tag.name}}</a>
                       {% endfor %}
   </div>
  </div>
 </li>
</ul>

I think you mispelled here:

instead of this:

{% for tag in post.tags.all %}

do this:

{% for tg in posts.tag.all %}

Hope this will help.

Back to Top