title: Using Django, How do I return all posts associated with a tag
body: <p>I'm trying to return all posts associated with a tag using Django. I have searched for a solution but can't find one. I tried to code something but it didn't work. Heres my code</p>
  
  <p>models.py</p>
  
  <pre><code>class Tag(models.Model):
      title = models.CharField(max_length=250)
      slug = models.SlugField(max_length=200, unique=True)
      timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
      updated = models.DateTimeField(auto_now=True, auto_now_add=False)
  
      def __str__(self):
          return self.title
  
      def get_absolute_url(self):
          return reverse("posts:tag_index", kwargs={"slug": self.slug})
  
      class Meta:
          ordering = ["-timestamp"]
  
  
  class Post(models.Model):
      user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
      slug = models.SlugField(unique=True)
      title = models.CharField(max_length=120)
      image = models.ImageField(upload_to=upload_location, null=True, blank=True,
                                width_field="width_field",
                                height_field="height_field")
      height_field = models.IntegerField(default=0)
      width_field = models.IntegerField(default=0)
      content = models.TextField()
      draft = models.BooleanField(default=False)
      publish = models.DateField(auto_now=False, auto_now_add=False)
      timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
      updated = models.DateTimeField(auto_now=True, auto_now_add=False)
      tags = models.ManyToManyField(Tag)
  
      objects = PostManager()
  
      def __str__(self):
          return self.title
  
      def get_absolute_url(self):
          return reverse("posts:detail", kwargs={"slug": self.slug})
  
      class Meta:
          ordering = ["-timestamp"]
  </code></pre>
  
  <p>posts/urls.py</p>
  
  <pre><code>from .views import post_list, post_create, post_detail, post_update, post_delete, sewp, tag_detail
  
  
  urlpatterns = [
      url(r'^$',   post_list, name='list'),
      url(r'^tag/(?P&lt;slug&gt;[\w-]+)/$', tag_detail, name="tag_index"),
      url(r'^create/$', post_create, name='create'),
      url(r'^sewp$', sewp, name='sewp'),
      url(r'^(?P&lt;slug&gt;[\w-]+)/$', post_detail, name='detail'),
      url(r'^(?P&lt;slug&gt;[\w-]+)/edit/$', post_update, name='update'),
      url(r'^(?P&lt;id&gt;\d+)/delete/$', post_delete, name='delete'),
  ]
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def tag_detail(request, slug=None):
  query = slug
  queryset_list = Post.objects.active()
  tags_list = queryset_list.filter(tags__icontains=query).distinct()
  
  
  instance = get_object_or_404(Tag, slug=slug)
  
  context = {
      "instance": instance,
      "tags_list": tags_list
  }
  return render(request, "posts/tag_index.html", context)
  </code></pre>
  
  <p>tag_index.html</p>
  
  <pre><code>{% extends 'posts/base.html' %}
  
  {% block content %}
  
  {{ instance }}
  {{ tags_list }}
  
  {% endblock content %}
  </code></pre>
  
  <p>I just want to return all the tags and paginate it.</p>
  
  <p>and can I modify the following code to paginate my results :</p>
  
  <pre><code>paginator = Paginator(queryset_list, 2)  # Show 25 contacts per page
  page_request_var = "page"
  page = request.GET.get(page_request_var)
  try:
      queryset = paginator.page(page)
  except PageNotAnInteger:
      # If page is not an integer, deliver first page.
      queryset = paginator.page(1)
  except EmptyPage:
      # If page is out of range (e.g. 9999), deliver last page of results.
      queryset = paginator.page(paginator.num_pages)
  context = {
          "queryset": queryset,
          "title": "Posts",
          "page_request_var": page_request_var,
          "today": today,
          "queryset_list": queryset_list,
          "paginator": paginator,
      }
  </code></pre>
  
  <p>any guidance or help on this will be appreciated</p>