title: Adding categories to a django blog
body: <p>I made a simple django blog and I wanted it to display posts according to the category the post is assigned to.
  But it doesn't work as intended when I filtere it</p>
  <p><a href="https://i.stack.imgur.com/tqUkF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tqUkF.png" alt="enter image description here" /></a></p>
  <p>instead of filtering the posts according to the category it shows the html code in the base.html file.</p>
  <p>My polls/urls.py file</p>
  <pre><code>from django.urls import path
  
  from .views import HomeView, ArticleDetailView, AddPostView, UpdatePostView
  from .views import DeletePostView, AddCategoryView, CategoryView
  
  urlpatterns = [
      path('', HomeView.as_view(), name='home'),
      path('article/&lt;int:pk&gt;/', ArticleDetailView.as_view(), name='article-view'),
      path('add_post/', AddPostView.as_view(), name='add_post'),
      path('add_category/', AddCategoryView.as_view(), name='add_category'),
      path('article/edit/&lt;int:pk&gt;/', UpdatePostView.as_view(), name='update_post'),
      path('article/&lt;int:pk&gt;/remove/', DeletePostView.as_view(), name='delete_post'),
      path('category/&lt;str:categories&gt;/', CategoryView, name='category')
  ]
  </code></pre>
  <p>My views.py file</p>
  <pre><code>from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
  from .models import Post, Category
  from .forms import PostForm, EditForm
  from django.urls import reverse_lazy
  from django.shortcuts import render
  
  
  class HomeView(ListView):
      model = Post
      template_name = &quot;home.html&quot;
      # ordering = [&quot;-post_date&quot;]
      ordering = [&quot;-id&quot;]
  
  
  class ArticleDetailView(DetailView):
      model = Post
      template_name = &quot;detail_view.html&quot;
  
  
  class AddPostView(CreateView):
      model = Post
      form_class = PostForm
      template_name = &quot;add_post.html&quot;
      # fields = &quot;__all__&quot;
  
  
  class AddCategoryView(CreateView):
      model = Category
      fields = &quot;__all__&quot;
      template_name = &quot;add_category.html&quot;
  
  
  def CategoryView(request, categories):
      category_posts = Post.objects.filter(category=categories)
      return render(request, &quot;categories.html&quot;, {&quot;categories&quot;: categories}, {'category_posts': category_posts})
  
  
  class UpdatePostView(UpdateView):
      model = Post
      form_class = EditForm
      template_name = &quot;update_post.html&quot;
      # fields = (&quot;title&quot;, &quot;title_tag&quot;, &quot;body&quot;)
  
  
  class DeletePostView(DeleteView):
      model = Post
      template_name = &quot;delete_post.html&quot;
      success_url = reverse_lazy(&quot;home&quot;)
  </code></pre>
  <p>My categories.html file</p>
  <pre><code>{% extends 'base.html' %}
  
  {% block content %}
  &lt;head&gt;
      &lt;title&gt;{{ categories }} category&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;h1&gt;{{ categories }} category&lt;/h1&gt;
  &lt;hr&gt;
  
  {% for post in category_posts %}
  &lt;ul&gt;
      &lt;li&gt;
          &lt;a href=&quot;{% url 'article-view' post.pk %}&quot;&gt;
              {{ post.title }}&lt;/a&gt; - 
          {{ post.category }} - 
          {{ post.author.first_name }}
          {{ post.author.last_name }} - {{ post.post_date }}&lt;br/&gt;
          &lt;small&gt;{{ post.body | slice:&quot;:359&quot; | safe }}&lt;/small&gt;
  
          {% if user.is_authenticated %}
          &lt;small&gt;&lt;a href=&quot;{% url 'update_post' post.pk %}&quot;&gt; (Edit) &lt;/a&gt;&lt;/small&gt; &lt;small&gt;&lt;a
              href=&quot;{% url 'delete_post' post.pk %}&quot;&gt;(Delete)&lt;/a&gt;&lt;/small&gt; &lt;br/&gt;
          {% endif %}
      &lt;/li&gt;
  &lt;/ul&gt;
  {% endfor %}
  
  {% endblock %}
  </code></pre>
  <p>My models.py file</p>
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  from django.urls import reverse    
  
  class Category(models.Model):
      name = models.CharField(max_length=255)
  
      def __str__(self):
          return (self.name)
  
      def get_absolute_url(self):
          return reverse(&quot;home&quot;)
  
  
  class Post(models.Model):
      title = models.CharField(max_length=255)
      title_tag = models.CharField(max_length=255)
      author = models.ForeignKey(User, on_delete=models.CASCADE)
      body = models.TextField(max_length=3500)
      post_date = models.DateField(auto_now_add=True)
      category = models.CharField(max_length=255, default=&quot;uncategorized&quot;)
  
      def __str__(self):
          return (self.title + &quot; | &quot; + str(self.author))
  
      def get_absolute_url(self):
          return reverse(&quot;home&quot;)
  </code></pre>
  <p>Thank you.</p>