How to show the category on the frontend with using django tag template?

I am the beginner of writing program and using Django, now I would like to display the category on the front-end. I have created a foreign-key which named “topic” as a category. It is successfully access to the topic(sub-category) and the post (by using pk)by entering URL. Although the URL config is work but I have no idea how to put it on the front-end by using template tag. I would like to create a catalog and when I clicked to the certain catalog then will show these posts which under the specific “topic”. I learned Django by the book “Django for beginners” and “Django for professional” however it does not cover such topic. And I watch online tutorial and I very confused and frustrated. (My Django version is 3.0.0)

Thanks for your help :)

The Article URL like that http://127.0.0.1:8000/django/1 Topic (FK) (Post PK)

My Model

from django.db import models
from ckeditor.fields import RichTextField

class Topic(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=50, default='self.name')

    def get_absolute_url(self):
        return reverse('topic',
                       args=[self.slug])

    def __str__(self):
        return self.name

class Post(models.Model):
    title =  models.CharField(max_length=50)
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    content = RichTextField()
    topic = models.ForeignKey(Topic, default=1, on_delete=models.SET_DEFAULT)

    def __str__(self):
        return self.title

View

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post

class Home(ListView):
   model = Post
   template_name = 'home.html'

class TopicList(ListView):
   model = Post
   template_name = 'topiclist.html'

class PostDetail(DetailView):
   model = Post
   template_name = 'detail.html'
# Create your views here.
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post

class Home(ListView):
   model = Post
   template_name = 'home.html'

class TopicList(ListView):
   model = Post
   template_name = 'topiclist.html'

class PostDetail(DetailView):
   model = Post
   template_name = 'detail.html'

urls

from django.urls import path
from .views import Home, PostDetail, TopicList


urlpatterns = [
   path('', Home.as_view(), name='home'),
   path('<slug:topic>/<int:pk>', PostDetail.as_view(), name='detail'),
   path('<slug:topic>/', TopicList.as_view(), name='topiclist'),

1 answer

  • answered 2020-06-02 09:21 CodeWriter

    You would write a view where you filter the rows where topic is matched. Then pass the returned query to your template:

    Topic = Topic.objects.get(name = 'coding')
    Posts = Post.objects.filter(topic=Topic)
    return render(request, 'catalog.html', {'posts': Posts})
    

    Then in your template file:

    {% for post in posts %}
        <li>{{ post.title }}</li>
    {% endfor %}