Sara Nancy
Sara Nancy

Reputation: 37

How show category title on django

My problem is that in the category pages, title, meta title and meta description are not displayed.

I have shown it in the image I uploaded below.

How i can fix it?

Thank you

modles.py

class Category(models.Model):
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE)
    title = models.CharField(max_length=70, unique=True)
    slug = models.SlugField(max_length=70, unique=True)
    description = RichTextUploadingField(blank=True, null=True)
    meta_title = models.CharField(max_length=120, unique=True, blank=True, null=True)
    meta_description = models.TextField(max_length=175, blank=True, null=True)

    def __str__(self):
       return self.title
    
    class MPTTMeta:
       order_insertion_by = ['title']

views.py

class PostCategoryView(generic.ListView):
    template_name = 'posts_category.html'
    paginate_by = 9

    def get_queryset(self):
        slug = self.kwargs.get('slug')
        return Post.objects.filter(category__slug=slug, status=1)

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['category'] = Category.objects.all()
        return context

category template

{% extends 'shared/_MainLayout.html' %}
{% load static %}
{% block head %}
{% block title %}**{{ category.meta_title }}**{% endblock %}
{% block description %}**{{ category.meta_description }}**{% endblock %}
{% endblock %}
{% block content %}
                <div class="page-header page-header-style-1 text-center mt-50">
                    <div class="container">
                        <h2><span class="color2">**{{ category.title }}**</span></h2>
                        <div class="breadcrumb">
                            <a href="/" rel="nofollow">Home</a> &nbsp; > &nbsp;
                            <span></span><a href="/{{ category.slug }}/" rel="nofollow">**{{ category.title }}**</a>
                        </div>
                    </div>
                </div>
{% endblock %}

image of problem

Upvotes: 0

Views: 236

Answers (1)

barbaart
barbaart

Reputation: 867

You can set the context_object_name to acces the object in the template. Or you can use the default name {{ object_list }}.

For example:

class PostCategoryView(generic.ListView):
    template_name = 'posts_category.html'
    paginate_by = 9
    context_object_name = Example
    model = Category

    def get_queryset(self):
        slug = self.kwargs.get('slug')
        return Post.objects.filter(category__slug=slug, status=1)

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['category'] = Category.objects.all()
        return context

Now you can use this in your template:

{% for category in Example %}
    <li>{{ category.title }}</li>
{% endfor %}

If you do not set the context_object_name you can use the default name 'object_list' like this:

{% for category in object_list %}
    <li>{{ category.title }}</li>
{% endfor %}

You also have to set your model in the ListView like: model = Category

Upvotes: 1

Related Questions