title: Django - Filter Model Objects (Posts) Using Form
body: <p>So I have a model called Blog</p>
  
  <pre><code>class Blog(models.Model):
      tagoptions = (
          (u'Project', u'Project'),
          (u'IT', u'IT'),
          (u'Robotics', u'Robotics'),
              . . .
          (u'-', u'-'),
      )
      user = models.TextField(blank=True, null=True)
      title = models.CharField(max_length=50, default='')
      context = models.TextField(max_length=5000, default='')
      ireceived = models.IntegerField(default=0, blank=True, null=True)
      personnelneeded = models.IntegerField(default=1)
      datewritten = models.DateField(default=datetime.date.today, blank=True, null=True)
      tags = models.CharField(max_length=100, choices=tagoptions, default='-')
  
      def __str__(self):
          return str(self.user) + ": id" + str(self.id) + " - title: " + str(self.title)
  </code></pre>
  
  <p>and a form for filtering the objects using "tags"</p>
  
  <pre><code>class FilterOption(forms.Form):
      tagoptions = (
          (u'showall', u'Show All'),
          (u'Project', u'Project'),
          (u'IT', u'IT'),
              . . .
          (u'-', u'-'),
      )
      tags = forms.ChoiceField(choices=tagoptions, initial="showall")
      tags.widget.attrs.update({'style' : 'color: black;'})
  </code></pre>
  
  <p>This is the part of the form for filtering in my template.</p>
  
  <pre><code>    &lt;form method="post" action="{% url 'posts' %}"&gt;
           {% csrf_token %}
           &lt;p&gt;{{ form.tags }} &lt;input type="submit" name="filter" value="Filter"/&gt;&lt;/p&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>Here is what I use in my template to display the top 10 most recent posts.</p>
  
  <pre><code>{% for p in postshow|slice:":10" %}
        &lt;br&gt;&lt;br&gt;
        &lt;div class="postdisplay"&gt;
          &lt;h1&gt;&lt;a class="stuffurl" href="{% url 'posted' postid=p.id %}"&gt;&lt;u&gt;{{ p.title }}&lt;/u&gt;&lt;/a&gt;&lt;/h1&gt;
          &lt;h4&gt;Post by &lt;a class="stuffurl" href="{% url 'user' user_login_name=p.user %}"&gt;&lt;u&gt;{{ p.user }}&lt;/u&gt;&lt;/a&gt;, {{ p.datewritten}}&lt;/h4&gt;
          &lt;h4&gt;Tag: {{ p.tags }}
        &lt;/div&gt;
        &lt;br&gt;&lt;br&gt;
  {% endfor %}
  </code></pre>
  
  <p>Then finally in my views I have</p>
  
  <pre><code>def posts(request):
      postshow = Blog.objects.order_by('-id')
      if request.method == 'POST':
          form = FilterOption()
  
          if form.is_valid():
              filteropt = form.cleaned_data['tags']
              if filteropt != showall:
                  postshow = Blog.objects.order_by('-id')
  
              else:
                  postshow = Blog.objects.filter(tags=filteropt).order_by('-id')
  
      else:
          form = FilterOption()
  
      context = {'postshow' : postshow, 'form' : form}
      return render(request, 'home/posts.html', context)
  </code></pre>
  
  <p>When ever I submit the form, it's supposed to check if the input says show all or not. If it does then it does not filter anything and if not it filters the objects so that only the objects with the tag of the input gets shown. However, the code seems to be stuck at form.is_valid (I checked using print). Another possible problem is that even if it worked pass form.is_valid, the filter won't work since the input is refreshed whenever you submit the form.</p>
  
  <p>It will be greatly appreciated if someone can help me fix my code so that I can select a tag and the post will be filtered and if I select "show all", it shows all posts without filtering them.</p>