How to access 2 models attributes through foreignkey in django

Django learner here, I am trying to build a simple blog website in which i have created two models:

one is Post:

class Post(models.Model):
    title = models.CharField(max_length=255)
    author= models.ForeignKey(User, null=True, blank=True , on_delete=models.CASCADE)
    article = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    slug  = AutoSlugField(populate_from='title', unique=True, null=True, default=None)
    category = models.ForeignKey(Category, null=True, blank=True,  on_delete=models.CASCADE )
    
    def __str__(self):
       return self.title 

second is category:

class Category(models.Model):
    categories = models.CharField(max_length=24, blank=True)

    def __str__(self):
       return self.categories

all i am trying to do is to show Category on home page, and when someone click on any category it will open up all the post related to that category.

This is home.html :

{% extends 'blog_pages/base.html' %}

{% block content %}
    <div class = "container p-3">
        <h3> This is your home page</h3>
    </div>
     <div class = "container p-1">
    <table class="table table-hover table-bordered">
    <thead>
        <tr>
            <th scope="col">Categories</th>
            <th scope="col">About</th>
        </tr>
    </thead>
    <tbody>
        {% for c in cat %} 
        <tr>
            <th scope="row"><a href="{% url 'all_articles' c %}" ><p> {{c}}</P></a></th>
            <td> How you can win in life</td>
        </tr>
        {% endfor %}
   
  </tbody>
</table>
</div>

this is views.py :

def home(request):
    cat = Category.objects.all()
    return render(request, 'blog_pages/home.html',{'cat': cat})

def all_articles(request, c):
    post = Post.objects.filter(category__contains = c).values()
    return render(request,"blog_pages/all_articles.html",{'post':post})

I am getting this error " FieldError( django.core.exceptions.FieldError: Related Field got invalid lookup: contains"

i have tried many possible ways to solve this problem, but nothing works.

Back to Top