Django. I want it to be moved within that category list

I created a bulletin board app using DJango. These bulletins are categorized into categories.

If you click on a text in the full list page, navigate to the detail page, and select Previous and Next, of course, it will move normally.

What I've been struggling with for a few days is that when I click on a text in a particular category list and go to the detail page, I want it to be moved within that category list if I choose the previous and next text.

models.py

from django.db import models
from django.contrib.auth.models import User
import os

class Category(models.Model):
    name = models.CharField(max_length=20, unique=True)
    slug = models.SlugField(max_length=200, unique=True,  allow_unicode=True)
    description = models.TextField(default='카테고리 설명')

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return f'/mytube/category/{self.slug}/'

class Post(models.Model):
    title = models.CharField(max_length=100)
    hook_text = models.CharField(max_length=100, blank=True)
    content = models.TextField()

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL, default=5)

    video_id = models.CharField(max_length=50, blank=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, default='1')

    def __str__(self):
        return f'[{self.pk}] {self.title} :: {self.author}'

    def get_absolute_url(self):
        return f'/mytube/{self.pk}/'
    
    def prev(self):
        return self.get_previous_by_created_at()

    def next(self):
        return self.get_next_by_created_at()


views.py

from django.views.generic import ListView, DetailView
from .models import Post, Category

class PostList(ListView): #ListView 클래스를 PostList로 상속 
    model = Post
    template_name = 'mytube/post_list.html'
    ordering = ['-pk']
    paginate_by = 10  

    def get_context_data(self, **kwargs):
        context = super(PostList, self).get_context_data()
        context['all_post_count'] = self.get_queryset().count()
        context['categories'] = Category.objects.all()
        context['paginate_by'] = PostList.paginate_by
        return  context

class PostListByCategory(PostList):
    def get_queryset(self):
        slug = self.kwargs['slug']
        if slug == 'no_category':
            category = None
        else:
            category = Category.objects.get(slug=slug)
        return Post.objects.filter(category=category).order_by('-pk')

    def get_context_data(self, **kwargs):
        slug = self.kwargs['slug']
        category = Category.objects.get(slug=slug)

        context = super(PostListByCategory, self).get_context_data()
        #context['cate_post_etc'] = Post.objects.filter(category__name=category).order_by('-pk')

        slug = self.kwargs['slug']
        if slug == 'no_category':
            category = '미분류'
        else:
            category = Category.objects.get(slug=slug)
        context['category'] = category
        return context

class PostDetail(DetailView):
    model = Post
    template_name = 'mytube/post_detail.html'


Back to Top