How to direct from post to category page in Django?
NickName:mdev Ask DateTime:2023-01-07T22:48:42

How to direct from post to category page in Django?

I'm working on my Django blog. I have a trouble to redirect from post to category, when you open post you can click to category and when you click on category I want you to redirect you to category and show posts only from that category.

This part of my html code for post_detail.html

<div class="entry-meta meta-0 font-small mb-30"><a href="{{ category.get_absolute_url }}"><span class="post-cat bg-success color-white">{{ post.category}}</span></a></div>
<h1 class="post-title mb-30">
    {{ post.post_title }}
</h1>

This is models.py only class category


class Category(models.Model):
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="Created at")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="Updated at")
    category_name = models.CharField(max_length=255, verbose_name="Category name")
    slug = models.SlugField(max_length=200, unique=True)

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

    class Meta:
        verbose_name = "Category"
        verbose_name_plural = "Categories"
        ordering = ['category_name']

    def __str__(self):
        return self.category_name

in post_detail is defined like this (short view)


class Post(models.Model):
    ...
    post_title = models.CharField(max_length=200, verbose_name="Title")
    category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE)
    ...

    def __str__(self):
        return self.post_title

This is views.py

def post_detail(request, slug):
    latest_posts =  Post.objects.filter(created_at__lte=timezone.now()).order_by('created_at')[:9]
    post = get_object_or_404(Post, slug=slug)

    context = {'post': post, 'latest_posts': latest_posts}
    return render(request, 'post_detail.html', context)


def category_detail(request, pk):
    category = get_object_or_404(Category, pk=pk)

    return render(request, 'category_detail.html', {'category': category})

This is urls.py

from . import views
from django.urls import path

urlpatterns = [
    path('', views.home, name='home'),
    path('<slug:slug>/', views.post_detail, name='post_detail'),
    path('<slug:slug>/', views.category_detail, name='category_detail'),
]

Any idea why I'm not redirected to category_detail page?

Thanks in advance!

Copyright Notice:Content Author:「mdev」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/75041237/how-to-direct-from-post-to-category-page-in-django

More about “How to direct from post to category page in Django?” related questions

How to direct from post to category page in Django?

I'm working on my Django blog. I have a trouble to redirect from post to category, when you open post you can click to category and when you click on category I want you to redirect you to category...

Show Detail

How can i make post category list visible in all page in django

I am trying to make category name visible in every page. so for that I passed category model to every view. But here it is visible only in category_view.html page only. I want to make it visible

Show Detail

Receiving an error as follows - IntegrityError at /rango/category/Django/add_page/ NOT NULL constraint failed: rango_page.category_id

Here is the views.py from django.shortcuts import render from .models import Category , Page from .forms import CategoryForm , PageForm from django.http import HttpResponse # Create your views h...

Show Detail

Django post not showing on homepage, but in category list

I made a blog with Django. The blog lets the admin create posts and categories. I used bootstrap to align the posts next to each other. This is the homepage of my blog. When the posts reach the oth...

Show Detail

Django page & category names showing as objects

This code is showing page and category names as objects and not by their respective title. Its supposed to show the names and its showing page objects and category objects instead for all the titles

Show Detail

How to direct to a form page based on category chosen from drop down in codeigniter?

So I try to create a page for category with dropdown, but I can't direct it to the function for each category that I've chosen and load the form view. This is the view for choosing the category fir...

Show Detail

Django sitemap category page location not working

I made sitemap to my site but i have some problems. First problem is end of the sitemap.xml page, lots of codes in it, but how can i remove these lines. Here is my sitemap https://www.endustri.io/s...

Show Detail

Django get all post of a parent category

I'm a begginner in django 3 and i decide to create a simple blog to pratice what i learned . I created a post model with a foreignkey category , and a model category with a self foreign key .

Show Detail

Incorrect redirection for a single post category

I'm developing my personal blog; it has only two categories and I would like to have a specific list of posts for both categories. For this reason I've expand get_absolute_url as you can see below:

Show Detail

How can I make category view to list out all the post with related category in Django using class based views

I want to know how to make category page view using class based view I know how to make this in function based view by using get_object_or_404(category, slug=None) But I am confused how to do this ...

Show Detail