title: Filtering tagged blog posts using intermediate table in Django
body: <p>I am trying to render a page by filtering the articles by tags. I want the related articles to be displayed when a user clicks the tag in All-articles.html</p>
  <p>Im not sure how to go about it or how I would write the code.</p>
  <p>Should I create a path in <code>urls.py</code> like
  <code>path('tag/&lt;str:tagslug&gt;', views.tag, name='tag'),</code>
  so that I can access the url within the <code>views.py</code>?</p>
  <p>How would I write the ORM to filter articles by tags in <code>views.py</code>?</p>
  <p>Any help will be appreciated :) Thanks</p>
  <p><code>models.py</code></p>
  <pre><code>class ArticleTags(models.Model):
      article = models.ForeignKey('Articles', models.DO_NOTHING)
      tag = models.ForeignKey('Tags', models.DO_NOTHING)
  
  class Tags(models.Model):
      tag = models.CharField(unique=True, max_length=75)
      tagslug = models.SlugField(max_length=200, unique=True, default=None)
  
  class Articles(models.Model):
      title = models.CharField(max_length=155)
      metatitle = models.CharField(max_length=155)
      slug = models.SlugField(unique=True, max_length=155)
      summary = models.TextField(blank=True, null=True)
      field_created = models.DateTimeField(db_column='_created', blank=True, null=True)  
      cover = models.ImageField(upload_to=&quot;cover&quot;, blank=True, default='logo-00-06.png')
  
  </code></pre>
  <p><code>views.py</code></p>
  <pre><code>def allarticles(request):
      articles_list = Articles.objects.all()
      paginator = Paginator(articles_list, 12)
  
      page_number = request.GET.get('page')
      page_obj = paginator.get_page(page_number)
  
      context = {'page_obj': page_obj}
      return render(request, &quot;All-articles.html&quot;, context)
  </code></pre>
  <p><code>All-articles.html</code></p>
  <pre><code>{% for article in page_obj %}
      &lt;article&gt;
        &lt;div&gt;
          &lt;img
            src=&quot;media/{{ article.cover }}&quot;
            alt=&quot;menu item&quot;
            class=&quot;article-photo&quot;
          /&gt;
        &lt;/div&gt;
        &lt;header&gt;
          &lt;h4&gt;{{ article.title }}&lt;/h4&gt;
        &lt;/header&gt;
      &lt;/article&gt;
  {% endfor %}
  </code></pre>