title: Django Filtering A Blog Based on Tags
body: <p>I'm trying to filter my blog based on tags. I've managed to work out how to display all the tags, however now I want the user to have the ability to click on a tag and bring up a filter list of blog posts based on that tag.</p>
  
  <p>I'm getting an error:</p>
  
  <pre><code>ValueError invalid literal for int() with base 10: 'testtag'
  </code></pre>
  
  <p>url.py </p>
  
  <pre><code>urlpatterns = [
      # Examples: /blogs
      url(r'^tags$', 'blog.views.tags', name='tags'),
      url(r'^tags/(?P&lt;blog_tagslug&gt;[\w-]+)$', 'blog.views.tagslist', name='tagslist'), 
  </code></pre>
  
  <p>views.py </p>
  
  <pre><code>def tags(request):
      blog_obj = Tag.objects.all()
      paginator = Paginator(blog_obj, 5)
  
      page = request.GET.get('page')
  
      try:
          blog_list = paginator.page(page)
      except PageNotAnInteger:
          # If page is not an integer, deliver first page.
          blog_list = paginator.page(1)
      except EmptyPage:
          # If page is out of range (e.g. 9999), deliver last page of results.
          blog_list = paginator.page(paginator.num_pages)
  
      template_hash = {'blog_list':blog_list}
      return render(request,'blog/tags.html', template_hash)
  
  def tagslist(request, blog_tagslug):
      #blog = Blog.objects.get(pk=blog_id)
      #blog = Tag.objects.all().filter(tagslug=blog_tagslug)
      blog = Blog.objects.all().filter(tags=blog_tagslug)
  
      paginator = Paginator(blog, 2)
  
      page = request.GET.get('page')
  
      try:
          blog_list = paginator.page(page)
      except PageNotAnInteger:
          # If page is not an integer, deliver first page.
          blog_list = paginator.page(1)
      except EmptyPage:
          # If page is out of range (e.g. 9999), deliver last page of results.
          blog_list = paginator.page(paginator.num_pages)
  
      template_hash = {'blog_list':blog_list}
      return render(request,'blog/index.html', template_hash)
  </code></pre>
  
  <p>tags.html (works fine)</p>
  
  <pre><code>&lt;h1&gt;All Tags&lt;/h1&gt;
  {% if blog_list %}
      &lt;ul&gt;
      {% for blog in blog_list %}
          {{ blog.image.thumbnail.url }}
  
          &lt;li&gt;&lt;a href="{% url "tagslist" blog.tagslug %}"&gt;{{ blog.tagslug }}&lt;/a&gt;&lt;/li&gt;
          {{ blog.tags.all|join:", " }} &lt;br&gt;
      {% endfor %}
      &lt;/ul&gt;
  {% else %}
      &lt;p&gt;No blogs are available.&lt;/p&gt;
  {% endif %}
  
  &lt;div class="pagination"&gt;
      &lt;span class="step-links"&gt;
          {% if blog_list.has_previous %}
              &lt;a href="?page={{ blog_list.previous_page_number }}"&gt;previous&lt;/a&gt;
          {% endif %}
  
          &lt;span class="current"&gt;
              Page {{ blog_list.number }} of {{ blog_list.paginator.num_pages }}.
          &lt;/span&gt;
  
          {% if blog_list.has_next %}
              &lt;a href="?page={{ blog_list.next_page_number }}"&gt;next&lt;/a&gt;
          {% endif %}
      &lt;/span&gt;
  &lt;/div&gt;   
  </code></pre>
  
  <p>tagslist.html (doesn't work gets error)</p>
  
  <pre><code>&lt;h1&gt;Blog Index&lt;/h1&gt;
  {% if blog_list %}
      &lt;ul&gt;
      {% for blog in blog_list %}
          {{ blog.image.thumbnail.url }}
  
          &lt;li&gt;&lt;a href="{% url "show" blog.id blog.slug %}"&gt;{{ blog.blog_title }}&lt;/a&gt;&lt;/li&gt;
          {{ blog.tags.all|join:", " }} &lt;br&gt;
      {% endfor %}
      &lt;/ul&gt;
  {% else %}
      &lt;p&gt;No blogs are available.&lt;/p&gt;
  {% endif %}
  
  &lt;div class="pagination"&gt;
      &lt;span class="step-links"&gt;
          {% if blog_list.has_previous %}
              &lt;a href="?page={{ blog_list.previous_page_number }}"&gt;previous&lt;/a&gt;
          {% endif %}
  
          &lt;span class="current"&gt;
              Page {{ blog_list.number }} of {{ blog_list.paginator.num_pages }}.
          &lt;/span&gt;
  
          {% if blog_list.has_next %}
              &lt;a href="?page={{ blog_list.next_page_number }}"&gt;next&lt;/a&gt;
          {% endif %}
      &lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>models.py </p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  from django.core.urlresolvers import reverse
  from django.conf import settings
  
  # Create your models here.
  
  class Image(models.Model):
       name = models.CharField(max_length = 255)
       image = models.ImageField(upload_to="%Y/%m/%d",max_length=500,blank=True,null=True)
       thumbnail = models.ImageField(upload_to="%Y/%m/%d",max_length=500,blank=True,null=True)
  
       def __str__(self):
          return self.name
  
  
  class Tag(models.Model):
      tagslug = models.SlugField(max_length=200, unique=True)
  
      def __str__(self):
          return self.tagslug
  
  class Author(models.Model):
      authorname = models.CharField(max_length=200, null=True)
  
      def __str__(self):
          return self.authorname
  
  
  class Blog(models.Model):
      blog_title = models.CharField(max_length = 200)
      author = models.ForeignKey(Author)
      image = models.ForeignKey(Image)    
      blog_content = models.TextField()
      slug = models.SlugField(max_length=200, unique=True)
      publish = models.BooleanField(default=True)
      created = models.DateTimeField(auto_now_add=True)
      modified = models.DateTimeField(auto_now=True)
      tags = models.ManyToManyField(Tag)
      user_id = models.ForeignKey(User)
  
      def __str__(self):
          return self.blog_title
  
  class Comment(models.Model):
      comment_content = models.TextField()
      created = models.DateTimeField(auto_now_add=True)
      modified = models.DateTimeField(auto_now=True)  
      blog_id = models.ForeignKey(Blog)
      user_id = models.ForeignKey(User)
  
      def __str__(self):
          return '{0}, {1}'.format(self.comment_content, self.created)
  </code></pre>